Skip to content

Commit

Permalink
april 23
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSpiridonov committed Apr 23, 2021
1 parent 7ee624d commit 3ba5775
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions Challenges/2021/April-LeetCoding-Challenge.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ None
||Title|Solution|Difficulty|
| ----: | --- | --- | --- |
|554.|[Brick Wall](https://leetcode.com/problems/brick-wall/)|[Python](/Medium/554.BrickWall.py)|Medium|
|696.|[Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/)|[Python](/Easy/696.CountBinarySubstrings.py)|Easy|

## License
The code is open-source and licensed under the [MIT License](/LICENSE).
56 changes: 56 additions & 0 deletions Easy/696.CountBinarySubstrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'''
Give a string s, count the number of non-empty (contiguous)
substrings that have the same number of 0's and 1's,
and all the 0's and all the 1's in these substrings are
grouped consecutively.
Substrings that occur multiple times are counted the
number of times they occur.
Example:
Input: "00110011"
Output: 6
Explanation: There are 6 substrings that have equal number
of consecutive 1's and 0's: "0011", "01",
"1100", "10", "0011", and "01".
Notice that some of these substrings repeat
and are counted the number of times they occur.
Also, "00110011" is not a valid substring
because all the 0's (and 1's) are not grouped
together.
Example:
Input: "10101"
Output: 4
Explanation: There are 4 substrings: "10", "01", "10",
"01" that have equal number of consecutive
1's and 0's.
Note:
- s.length will be between 1 and 50,000.
- s will only consist of "0" or "1" characters.
'''
#Difficulty: Easy
#90 / 90 test cases passed.
#Runtime: 300 ms
#Memory Usage: 14.5 MB

#Runtime: 300 ms, faster than 7.82% of Python3 online submissions for Count Binary Substrings.
#Memory Usage: 14.5 MB, less than 74.34% of Python3 online submissions for Count Binary Substrings.

class Solution:

def countBinarySubstrings(self, s: str) -> int:
count = 0
for i in range(len(s)-1):
if s[i] != s[i+1]:
count += self.count(s, i, i+1, s[i], s[i+1])
return count

def count(self, s: str, i: int, j: int, l: str, r: str) -> int:
while i >= 0 and j < len(s) and s[i] == l and s[j] == r:
i -= 1
j += 1
return (j-i)//2
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Python solutions of LeetCode problems.
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
![Problems Solved](https://img.shields.io/badge/problems%20solved-545%2F1665-orange)&nbsp;
![Problems Solved](https://img.shields.io/badge/problems%20solved-546%2F1665-orange)&nbsp;
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
<br><br>
Expand All @@ -22,7 +22,7 @@ In this repository provided my Python solutions of LeetCode problems.
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 27/31
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 23/28
- [March LeetCoding Challenge](/Challenges/2021/March-LeetCoding-Challenge.md) - 23/31
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 17/30
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 18/30
<br><br>
## Solutions
*P.S. If you like this, please leave me a star.*
Expand Down Expand Up @@ -301,6 +301,7 @@ In this repository provided my Python solutions of LeetCode problems.
|678.|[Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/)|[Python](https://github.com/YuriSpiridonov/30-Day-LeetCoding-Challenge/blob/master/Week%203/ValidParenthesisString.py)|Medium|
|682.|[Baseball Game](https://leetcode.com/problems/baseball-game/)|[Python](/Easy/682.BaseballGame.py)|Easy||
|690.|[Employee Importance](https://leetcode.com/problems/employee-importance/)|[Python](/Easy/690.EmployeeImportance.py)|Easy|`BFS`|
|696.|[Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/)|[Python](/Easy/696.CountBinarySubstrings.py)|Easy|
|700.|[Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/)|[Python](/Easy/700.SearchinaBinarySearchTree.py)|Easy|`Binary Search Tree`, `Recursion`|
|701.|[Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/)|[Python](/Medium/701.InsertintoaBinarySearchTree.py)|Medium|`Binary Search Tree`, `Recursion`|
|703.|[Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/)|[Python](/Easy/703.KthLargestElementinaStream.py)|Easy|brute force|
Expand Down

0 comments on commit 3ba5775

Please sign in to comment.