Check if Binary String Has at Most One Segment of Ones

LeetCode Daily – #1784 Check if Binary String Has at Most One Segment of Ones [Easy] The Insight: Since the string always starts with '1' (no leading zeros), the only way a second segment of ones can appear is if a '0' comes BEFORE another '1' — i.e., the substring "01" exists in the string. So the entire problem reduces to one check: → Does "01" appear in the string? If yes → false. If no → true. Time: O(N) | Space: O(1) This is one of those problems where stripping away the noise leads to an almost trivially elegant solution. The key was recognizing the constraint — no leading zeros — which guarantees the string always opens with a '1', making "01" the only possible signal of a broken segment. Sometimes the best code is the code you don't write. 🚀 #LeetCode #CodingChallenge #CPlusPlus #ProblemSolving #Programming #SoftwareEngineering #POTD #DSA

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories