Checking Binary Strings for One Segment of Ones

DSA Challenge – Day 21 🚀 Continuing my Data Structures & Algorithms journey, today I solved the problem “Check if Binary String Has at Most One Segment of Ones.” 🔎 Problem Summary: Given a binary string s without leading zeros, determine whether it contains at most one contiguous segment of '1's. 💡 Approach: The key observation is that if multiple segments of '1' exist, the string will contain the pattern "01". So, by checking whether the string contains "01", we can easily determine if there is more than one segment of '1'. If "01" is present → multiple segments → return false If "01" is absent → only one segment → return true ⚡ Time Complexity: O(n) 📦 Space Complexity: O(1) 📈 Key Takeaway: Sometimes the most efficient solutions come from identifying simple patterns within the data rather than overcomplicating the logic. Looking forward to solving more problems and strengthening my problem-solving skills every day. #DSA #ProblemSolving #Java #CodingChallenge #SoftwareDevelopment #LearningInPublic

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories