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

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:

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…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store