From c0204ce8fd5b813fa55f3420ca449ff45de6c4e0 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Tue, 14 Jan 2025 15:54:24 +0900 Subject: [PATCH] add solution: valid-parentheses --- valid-parentheses/dusunax.py | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 valid-parentheses/dusunax.py diff --git a/valid-parentheses/dusunax.py b/valid-parentheses/dusunax.py new file mode 100644 index 000000000..f7fbee60f --- /dev/null +++ b/valid-parentheses/dusunax.py @@ -0,0 +1,41 @@ +''' +# 20. Valid Parentheses + +use stack data structure to perform as a LIFO + +## Time and Space Complexity + +``` +TC: O(n) +SC: O(n) +``` + +#### TC is O(n): +- iterating through the string just once to check if the parentheses are valid. = O(n) + +#### SC is O(n): +- using a stack to store the parentheses. = the worst case is O(n) +- using a map to store the parentheses. = O(1) + +> for space complexity, fixed space is O(1). +> 👉 parentheses_map is fixed and its size doesn't grow with the input size. +> 👉 if the map has much larger size? the space complexity is still O(1). +''' + +class Solution: + def isValid(self, s: str) -> bool: + stack = [] # SC: O(n) + parentheses_map = { # SC: O(1) + "(": ")", + "{": "}", + "[": "]" + } + + for char in s: # TC: O(n) + if char in parentheses_map: + stack.append(char) + else: + if len(stack) == 0 or parentheses_map[stack.pop()] != char: + return False + + return not stack