Leetcode 2124: Check if All A’s Appears Before All B’s

Pierre-Marie Poitevin
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', return true if every 'a' appears before every 'b' in the string. Otherwise, return false.

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…

--

--