Skip to content

Commit 0cf730d

Browse files
authored
Added Check if Binary String Has at Most One Segment of Ones.java
1 parent bcd77f6 commit 0cf730d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean checkOnesSegment(String s) {
3+
boolean foundContiguousSegment = false;
4+
int idx = 0;
5+
int n = s.length();
6+
while (idx < n) {
7+
int count = 0;
8+
while (idx < n && s.charAt(idx) == '1') {
9+
count++;
10+
idx++;
11+
}
12+
if (count >= 1) {
13+
if (foundContiguousSegment) {
14+
return false;
15+
}
16+
foundContiguousSegment = true;
17+
} else {
18+
idx++;
19+
}
20+
}
21+
return foundContiguousSegment;
22+
}
23+
}

0 commit comments

Comments
 (0)