Leetcode Problem 1003. Check If Word Is Valid After Substitutions
1003. Check If Word Is Valid After Substitutions
Leetcode Solutions
Using Stack to Validate String
Initialize an empty stack.
Iterate over each character in the string.
If the current character is 'a', push it onto the stack.
If the current character is 'b', check if the stack is not empty and the top is 'a'. If true, pop 'a' and push 'b'. If not, return false.
If the current character is 'c', check if the stack is not empty and the top is 'b'. If true, pop 'b' and check if the next top is 'a'. If true, pop 'a'. If any condition fails, return false.
After the loop, check if the stack is empty. If it is, return true; otherwise, return false.