Given a binary string s
without leading zeros, return true
if s
contains at most one contiguous segment of ones. Otherwise, return false
.
Example:
Input: s = "110"
Output: true
Approach:
C++
#include <bits/stdc++.h>using namespace std;bool checkOnesSegment(string s){int n = s.size();int i = 0;while (i < n && s[i] == '1'){i++;}while (i < n && s[i] == '0')i++;if (i == n)return true;return false;}int main(){string s = "110";if (checkOnesSegment(s))cout << "true";elsecout << "false";return 0;}
No comments:
Post a Comment