Skip to content

Commit 7be03ee

Browse files
solveslonger contiguous segment of ones than zeros
1 parent 004e733 commit 7be03ee

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@
452452
| 1854 | [Maximum Population Year](https://leetcode.com/problems/maximum-population-year) | [![Java](assets/java.png)](src/MaximumPopulationYear.java) | |
453453
| 1859 | [Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence) | [![Java](assets/java.png)](src/SortingTheSentence.java) | |
454454
| 1863 | [Sum of All Subset XOR Totals](https://leetcode.com/problems/sum-of-all-subset-xor-totals) | [![Java](assets/java.png)](src/SumOfAllSubsetXORTotals.java) | |
455-
| 1869 | [Longer Contiguous Segments of Ones than Zeros](https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros) | | |
455+
| 1869 | [Longer Contiguous Segments of Ones than Zeros](https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros) | [![Java](assets/java.png)](src/LongerContiguousSegmentOfOnesThanZeros.java) | |
456456
| 1876 | [Substrings of Size Three with Distinct Characters](https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters) | | |
457457
| 1880 | [Check if Word Equals Summation of Two Words](https://leetcode.com/problems/check-if-word-equals-summation-of-two-words) | | |
458458
| 1886 | [Determine Whether Matrix Can Be Obtained By Rotation](https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation) | | |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class LongerContiguousSegmentOfOnesThanZeros {
6+
public boolean checkZeroOnes(String s) {
7+
return lengthContiguousSegment(s, '1') > lengthContiguousSegment(s, '0');
8+
}
9+
10+
private int lengthContiguousSegment(String s, char c) {
11+
int maxLen = 0;
12+
for (int i = 0, current = 0 ; i < s.length() ; i++) {
13+
if (s.charAt(i) == c) current++;
14+
else current = 0;
15+
maxLen = Math.max(maxLen, current);
16+
}
17+
return maxLen;
18+
}
19+
}

0 commit comments

Comments
 (0)