We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 188c59a commit c1524b5Copy full SHA for c1524b5
src/leetcode2018/CountBinarySubstrings.java
@@ -1,22 +1,21 @@
1
package leetcode2018;
2
3
public class CountBinarySubstrings {
4
-
+
5
public int countBinarySubstrings(String s) {
6
int t=0;
7
- for(int i =0; i< s.length(); i++){
8
- int n=i+1;
9
- while(n< s.length() && s.charAt(n) == s.charAt(i)){
10
- n++;
11
- }
12
- int m=n+1;
13
- while(m< s.length() && s.charAt(m) == s.charAt(n)){
14
- m++;
15
16
- if((n-i)==(m-n)){
17
- t++;
18
+ int n=0;
+ int m=1;
+ for(int i = 1; i< s.length(); i++){
+ if(s.charAt(i-1) == s.charAt(i)){
+ m++;
+ }else{
+ t+= Math.min(n, m);
+ n= m;
+ m=1;
+ }
19
}
20
return t;
21
22
0 commit comments