1491. Number of Times Binary String Is Prefix-Aligned
My accepted Python solution to LeetCode problem 1491, Number of Times Binary String Is Prefix-Aligned, running in 35ms.
- Difficulty: Medium
- Python
- Runtime 35ms
- Memory 22.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 35ms, memory 22.5MB, accepted 2025-12-29.
class Solution:
def numTimesAllBlue(self, flips: List[int]) -> int:
count = 0
max_flip = 0
for i, flip in enumerate(flips):
max_flip = max(max_flip, flip)
# After i+1 flips, if max_flip equals i+1, string is prefix-aligned
if max_flip == i + 1:
count += 1
return count