Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 654791c

Browse files
committedNov 22, 2021
solves binary substing
1 parent 3a43d0b commit 654791c

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed
 

‎README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-167/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-167/2081-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-167/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-168/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-168/2081-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-168/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
88

@@ -188,7 +188,7 @@
188188
| 687 | [Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path) | |
189189
| 690 | [Employee Importance](https://leetcode.com/problems/employee-importance) | |
190190
| 693 | [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits) | [![Java](assets/java.png)](src/BinaryNumberWithAlternatingBits.java) [![Python](assets/python.png)](python/binary_number_with_alternating_bits.py) |
191-
| 696 | [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings) | |
191+
| 696 | [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings) | [![Java](assets/java.png)](src/CountBinarySubstrings.java) [![Python](assets/python.png)](python/count_binary_substrings.py) |
192192
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array) | |
193193
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree) | |
194194
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream) | |

‎python/count_binary_substrings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countBinarySubstrings(self, s: str) -> int:
3+
binarySubstrings, previous, current = 0, 0, 1
4+
index = 1
5+
while index < len(s):
6+
if s[index - 1] != s[index]:
7+
binarySubstrings += min(previous, current)
8+
previous, current = current, 1
9+
else: current += 1
10+
index += 1
11+
return binarySubstrings + min(previous, current)

‎src/CountBinarySubstrings.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class CountBinarySubstrings {
2+
public int countBinarySubstrings(String string) {
3+
int binarySubStrings = 0, prev = 0, current = 1;
4+
for (int index = 1 ; index < string.length() ; index++) {
5+
if (string.charAt(index - 1) != string.charAt(index)) {
6+
binarySubStrings += Math.min(prev, current);
7+
prev = current;
8+
current = 1;
9+
} else current++;
10+
}
11+
return binarySubStrings + Math.min(prev, current);
12+
}
13+
}

0 commit comments

Comments
 (0)
Please sign in to comment.