1784. Check if Binary String Has at Most One Segment of Ones LeetCode Solution
In this guide, you will get 1784. Check if Binary String Has at Most One Segment of Ones LeetCode Solution with the best time and space complexity. The solution to Check if Binary String Has at Most One Segment of Ones problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Check if Binary String Has at Most One Segment of Ones solution in C++
- Check if Binary String Has at Most One Segment of Ones solution in Java
- Check if Binary String Has at Most One Segment of Ones solution in Python
- Additional Resources
Problem Statement of Check if Binary String Has at Most One Segment of Ones
Given a binary string s without leading zeros, return true if s contains at most one contiguous segment of ones. Otherwise, return false.
Example 1:
Input: s = “1001”
Output: false
Explanation: The ones do not form a contiguous segment.
Example 2:
Input: s = “110”
Output: true
Constraints:
1 <= s.length <= 100
s[i] is either '0' or '1'.
s[0] is '1'.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
1784. Check if Binary String Has at Most One Segment of Ones LeetCode Solution in C++
class Solution {
public:
bool checkOnesSegment(string s) {
return s.find("01") == string::npos;
}
};
/* code provided by PROGIEZ */
1784. Check if Binary String Has at Most One Segment of Ones LeetCode Solution in Java
class Solution {
public boolean checkOnesSegment(String s) {
return !s.contains("01");
}
}
// code provided by PROGIEZ
1784. Check if Binary String Has at Most One Segment of Ones LeetCode Solution in Python
class Solution:
def checkOnesSegment(self, s: str) -> bool:
return '01' not in s
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.