[LeetCode]#2124. Check if All A’s Appears Before All B’s
Jan 18, 2022
Environment: Python 3.8
Key technique: if
Given a string s
consisting of only the characters 'a'
and 'b'
, return true
if every 'a'
appears before every 'b'
in the string. Otherwise, return false
.
Example 1:
Input: s = "aaabbb"
Output: true
Explanation:
The 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5.
Hence, every 'a' appears before every 'b' and we return true.
Analysis:
- check pattern “ba” is matched with s.
- If yes, return False. Else, return True.
Solution:
class Solution:
def checkString(self, s):
if "ba" in s:
return False
else:
return True
Submissions:
Reference:
https://leetcode.com/problems/check-if-all-as-appears-before-all-bs/discuss/1689073/Python-One-Liner