-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path696.cpp
More file actions
37 lines (36 loc) · 766 Bytes
/
Copy path696.cpp
File metadata and controls
37 lines (36 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// simple.cpp
class Solution {
int getCount(string &s, int l, int r, char lval) {
int len = 1;
while (l - len >= 0 && r + len < s.size() && s[l - len] == lval &&
s[r + len] != lval)
len++;
return len;
}
public:
int countBinarySubstrings(string s) {
int res = 0;
for (int i = 1; i < s.size(); ++i)
if (s[i - 1] != s[i])
res += getCount(s, i - 1, i, s[i - 1]);
return res;
}
};
// singleScan.cpp
class Solution2 {
public:
int countBinarySubstrings(string s) {
int pre = 0, cur = 1, res = 0;
for (int i = 1; i < s.size(); ++i) {
if (s[i] == s[i - 1])
++cur;
else {
pre = cur;
cur = 1;
}
if (cur <= pre)
++res;
}
return res;
}
};