Skip to content

Commit 3728f09

Browse files
solves check if binary string has at most one segment of ones
1 parent adcb007 commit 3728f09

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@
434434
| 1768 | [Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately) | [![Java](assets/java.png)](src/MergeStringsAlternately.java) | |
435435
| 1773 | [Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule) | [![Java](assets/java.png)](src/CountItemsMatchingARule.java) | |
436436
| 1779 | [Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate) | [![Java](assets/java.png)](src/FindNearestPointThatHasTheSameXOrYCoordinate.java) | |
437-
| 1784 | [Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones) | | |
437+
| 1784 | [Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones) | [![Java](assets/java.png)](src/CheckIfBinaryStringHasAtMostOneSegmentOfOnes.java) | |
438438
| 1790 | [Check if One String Swap Can Make Strings Equal](https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal) | | |
439439
| 1791 | [Find Center of Star Graph](https://leetcode.com/problems/find-center-of-star-graph) | | |
440440
| 1796 | [Second Largest Digit in a String](https://leetcode.com/problems/second-largest-digit-in-a-string) | | |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class CheckIfBinaryStringHasAtMostOneSegmentOfOnes {
2+
public boolean checkOnesSegment(String s) {
3+
int segments = 0;
4+
boolean isInSegment = false;
5+
for (int index = 0 ; index < s.length() ; index++) {
6+
if (!isInSegment && s.charAt(index) == '1') {
7+
isInSegment = true;
8+
segments++;
9+
} else if (isInSegment && s.charAt(index) == '0') {
10+
isInSegment = false;
11+
}
12+
}
13+
return segments <= 1;
14+
}
15+
}

0 commit comments

Comments
 (0)