Leetcode 1045 Solution

This article provides solution to leetcode question 1045 (check-if-word-is-valid-after-substitutions)

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