Skip to content

Commit 7aaa105

Browse files
add a solution for 696
1 parent 1c76034 commit 7aaa105

File tree

1 file changed

+21
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+21
-0
lines changed

src/main/java/com/fishercoder/solutions/_696.java

+21
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,25 @@ public int countBinarySubstrings(String s) {
3333
return (int) ans;
3434
}
3535
}
36+
37+
public static class Solution2 {
38+
/**
39+
* credit: https://leetcode.com/problems/count-binary-substrings/discuss/1172553/JS-Python-Java-C%2B%2B-or-Easy-Rolling-Count-Solution-w-Explanation
40+
*/
41+
public int countBinarySubstrings(String s) {
42+
int curr = 1;
43+
int prev = 0;
44+
int ans = 0;
45+
for (int i = 1; i < s.length(); i++) {
46+
if (s.charAt(i) == s.charAt(i - 1)) {
47+
curr++;
48+
} else {
49+
ans += Math.min(curr, prev);
50+
prev = curr;
51+
curr = 1;
52+
}
53+
}
54+
return ans + Math.min(curr, prev);
55+
}
56+
}
3657
}

0 commit comments

Comments
 (0)