Contiguous Array
nums holds only 0s and 1s. Find the longest contiguous stretch that has the same number of 0s and 1s, and return its length (0 if there is none).
Example 1
nums [0,1,0]
returns 2
[0, 1] or [1, 0], length 2.
Example 2
nums [1,1,1]
returns 0
No 0s, so 0.
Constraints
1 <= len(nums) <= 40- Each item is 0 or 1.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Hash maps problems
- Group AnagramsMedium
- Longest Consecutive SequenceMedium
- Longest Zero-Sum StretchMedium
- Rows That Match ColumnsMedium
- Shift FamiliesMedium
- Stretches With K Odd ValuesMedium
- Subarray Sum Equals KMedium
- Subarray Sums Divisible by KMedium