Skip to main content

Command Palette

Search for a command to run...

Check if Binary String Has at Most One Segment of Ones

Published
1 min read
Check if Binary String Has at Most One Segment of Ones
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given a binary string s ​​​​​without leading zeros, return true​​​ if s contains at most one contiguous segment of ones. Otherwise, return false.

LeetCode Problem - 1784 : Link | Click Here

class Solution {
    public boolean checkOnesSegment(String s) {
        // Initialize the result variable as true, assuming there is only one segment of '1's
        boolean result = true;

        // Iterate through the characters in the string, up to the second-to-last character
        for (int i = 0; i < s.length() - 1; i++) {
            // Retrieve the current character and the next character in the string
            char currentChar = s.charAt(i);
            char nextChar = s.charAt(i + 1);

            // Check if the current character is '0' and the next character is '1'
            if ((currentChar == '0') && (nextChar == '1')) {
                // If the condition is met, set the result to false and break out of the loop
                result = false;
                break;
            }
        }

        // Return the final result after iterating through the string
        return result;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.