Given a string s consisting of only the characters 'a' and 'b', return true _ifevery _'a' _appears beforeevery _'b'in the string. Otherwise, return false.
Input: s ="aaabbb"Output: trueExplanation:
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 returntrue.
Input: s ="abab"Output: falseExplanation:
There is an 'a' at index 2 and a 'b' at index 1.Hence, not every 'a' appears before every 'b' and we returnfalse.
We want to ensure that no ‘a’ appears after any ‘b’. As we scan the string, once we see a ‘b’, any subsequent ‘a’ would violate the rule. We can use a flag to track if a ‘b’ has been seen.