Intuition
The problem needs us to find if there are any a’s before any b’s. So we can keep track of when b’s start to appear. So once this flag is true and any a’s appear - we return True.
PS: This problem is pre-requisite to 1653 Minimum Deletions to Make String Balanced - so check that problem too.
Make sure to checkout 678 Valid Parenthesis String as it uses a similar strategy of counting
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since we would be visiting all characters in the string of length N.
-
Memory
The memory usage is since we are not using any extra data structure.
— A