Leetcode 1045 Solution
This article provides solution to leetcode question 1045 (check-if-word-is-valid-after-substitutions)
Access this page by simply typing in "lcs 1045" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/check-if-word-is-valid-after-substitutions
Solution
class Solution:
def isValid(self, S: str) -> bool:
s = []
for ch in S:
if len(s) >= 2 and s[-2] == 'a' and s[-1] == 'b' and ch == 'c':
s.pop(-1)
s.pop(-1)
else:
s.append(ch)
return not s