1. Description
Given a string s containing only ‘a’ and ‘b’, determine whether all occurrences of ‘a’ come before all occurrences of ‘b’.
Return true if this ordering holds, and false otherwise. A string containing only one of the two characters satisfies the condition.
2. Example
Example 1
Input: s = “aaabbb”
Output: true
Explanation: Every ‘a’ comes before every ‘b’.
Example 2
Input: s = “abab”
Output: false
Explanation: The ‘a’ at index 2 appears after the ‘b’ at index 1.
Example 3
Input: s = “bbb”
Output: true
Explanation: There are no occurrences of ‘a’, so the ordering condition holds.
3. Constraints
- 1 <= s.length <= 100
- s[i] is either ‘a’ or ‘b’.
4. Solutions
Simulation
n = str.size()
Time complexity: O(n)
Space complexity: O(1)
| |