Check if All A's Appears Before All B's

Given a string `s` made only of the characters `'a'` and `'b'`, return `true` if every `'a'` in `s` appears before every `'b'`. Otherwise, return `false`.

30:00

Check if All A's Appears Before All B's
easy
Free written solution
Topics
Companies

Given a string s made only of the characters 'a' and 'b', return true if every 'a' in s appears before every 'b'. Otherwise, return false.

Example 1:
Input: {"s":"aaabbb"}
Output: true
Constraints:
  • 1≤s.length≤1001 \leq s.\text{length} \leq 100

  • s[i] is either 'a' or 'b'.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"aaabbb"}

No 'b' seen yet, so the gate is open: an 'a' is still allowed.

s, with the gate at the first 'b'

a

0

a

1

a

2

b

3

b

4

b

5

two-state scanner

abbano 'b'yetseena 'b'falsereaching the end of s in either circle means true

seenB = false

Variables
VariableValue
seenBfalse
DepthFunction Call
Stack empty
0/8