Skip to content

1784. Check if Binary String Has at Most One Segment of Ones 👎

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
class Solution {
 public:
  bool checkOnesSegment(string s) {
    return s.find("01") == string::npos;
  }
};
1
2
3
4
5
class Solution {
  public boolean checkOnesSegment(String s) {
    return !s.contains("01");
  }
}
1
2
3
class Solution:
  def checkOnesSegment(self, s: str) -> bool:
    return '01' not in s