We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 1c76034 commit 7aaa105Copy full SHA for 7aaa105
src/main/java/com/fishercoder/solutions/_696.java
@@ -33,4 +33,25 @@ public int countBinarySubstrings(String s) {
33
return (int) ans;
34
}
35
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
57
0 commit comments