1784. Check if Binary String Has at Most One Segment of Ones
UnknownView on LeetCode
Time: O(n)
Space: O(1)
Advertisement
Approach
A second segment of ones is indicated by '01'; return !s.Contains("01").
Key Techniques
String
String problems range from simple character counting to complex pattern matching. Common approaches include two pointers, sliding window, prefix hashing, and the KMP algorithm. In C#, strings are immutable — use StringBuilder for efficient concatenation inside loops.
1784.cs
C#
// Approach: A second segment of ones is indicated by '01'; return !s.Contains("01").
// Time: O(n) Space: O(1)
public class Solution
{
public bool CheckOnesSegment(string s)
{
return !s.Contains("01");
}
}Advertisement
Was this solution helpful?