Leetcode 2124: Check if All A’s Appears Before All B’s
2 min readJan 2, 2022
In this leetcode problem, we want to know if all As appear before all Bs in a string.
Given a string
s
consisting of only the characters'a'
and'b'
, returntrue
if every'a'
appears before every'b'
in the string. Otherwise, returnfalse
.
For example:
"aaa" => true
"bbb" => true
"" => true
"ba" => false
"aababb" => false
To solve the problem, we read the string in order (from left to right). When we encounter the first b
, we change the our state, so that we can…