1784. Check if Binary String Has at Most One Segment of Ones
</> Solution
/**
* Problem: LeetCode 1784 - Check if Binary String Has at Most One Segment of Ones
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public boolean checkOnesSegment(String s) {
// Because s[0] == '1' (no leading zeros),
// any second segment of ones must be preceded by at least one '0'.
// If "01" appears anywhere in the string, there is more than one segment.
return !s.contains("01");
}
}
"""
Problem: LeetCode 1784 - Check if Binary String Has at Most One Segment of Ones
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
class Solution:
def checkOnesSegment(self, s: str) -> bool:
# Since s has no leading zeros, s starts with '1'.
# A second segment of '1's would require a '0' followed by a '1' ("01").
return "01" not in s
/**
* Problem: LeetCode 1784 - Check if Binary String Has at Most One Segment of Ones
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <string>
using namespace std;
class Solution {
public:
bool checkOnesSegment(string s) {
// Since s has no leading zeros (s[0] == '1'),
// more than one segment of ones exists if and only if "01" is present.
return s.find("01") == string::npos;
}
};
/**
* Problem: LeetCode 1784 - Check if Binary String Has at Most One Segment of Ones
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {string} s
* @return {boolean}
*/
var checkOnesSegment = function(s) {
// Since s starts with '1' (no leading zeros),
// a second segment of ones exists if and only if "01" appears in the string.
return !s.includes("01");
};