Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c1524b5

Browse files
Count Binary Substrings
1 parent 188c59a commit c1524b5

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

‎src/leetcode2018/CountBinarySubstrings.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package leetcode2018;
22

33
public class CountBinarySubstrings {
4-
4+
55
public int countBinarySubstrings(String s) {
66
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-
}
7+
int n=0;
8+
int m=1;
9+
for(int i = 1; i< s.length(); i++){
10+
if(s.charAt(i-1) == s.charAt(i)){
11+
m++;
12+
}else{
13+
t+= Math.min(n, m);
14+
n= m;
15+
m=1;
16+
}
1917
}
18+
t+= Math.min(n, m);
2019
return t;
2120
}
2221

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /