From d5e6259f5b809a2ee28519ad78e9ac203bfd9d27 Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Sat, 29 Nov 2025 20:33:08 -0500 Subject: [PATCH] Time: 0 ms (100%), Space: 17.6 MB (82.62%) - LeetHub --- .../0678-valid-parenthesis-string.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 0678-valid-parenthesis-string/0678-valid-parenthesis-string.py diff --git a/0678-valid-parenthesis-string/0678-valid-parenthesis-string.py b/0678-valid-parenthesis-string/0678-valid-parenthesis-string.py new file mode 100644 index 0000000..69d3703 --- /dev/null +++ b/0678-valid-parenthesis-string/0678-valid-parenthesis-string.py @@ -0,0 +1,61 @@ +class Solution: + def checkValidString(self, s: str) -> bool: + """ + Complexities: + Time: O(n) + Space: O(n) + + where n = len(s) + """ + + bracketCount = 0 + starCount = 0 + starIndecies = [] + usedStars = 0 + + # Forward pass - allocate as many *s to be ( + # - when allocating a star, always use the star that appears first + for i, l in enumerate(s): + if l == '(': + bracketCount += 1 + elif l == '*': + starCount += 1 + starIndecies.append(i) + elif bracketCount > 0: + bracketCount -= 1 + elif starCount > 0: + starCount -= 1 + usedStars += 1 + else: + return False + + # There are already matching opening/closing brackets + # - all stars are assigned empty string + if bracketCount == 0: + return True + + # There are more closed brackets needed than the amount of remaining stars + if bracketCount > 0 and bracketCount > starCount: + return False + + # Backward pass - allocate as many *s to be ) + # - the first `usedStars` stars are considered used from the forward pass + firstViableStar = starIndecies[usedStars] + bracketCount = 0 + starCount = 0 + for i in range(len(s) - 1, 0, -1): + l = s[i] + + if l == ')': + bracketCount += 1 + elif l == '*': + starCount += 1 if i >= firstViableStar else 0 + elif bracketCount > 0: + bracketCount -= 1 + elif starCount > 0: + starIndecies.pop(0) + starCount -= 1 + else: + return False + + return True \ No newline at end of file