Skip to content

Commit c1524b5

Browse files
committed
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy