1
+ '''
2
+ 20. Valid Parentheses
3
+
4
+ Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
5
+ An input string is valid if:
6
+ 1. Open brackets must be closed by the same type of brackets.
7
+ 2. Open brackets must be closed in the correct order.
8
+ 3. Every close bracket has a corresponding open bracket of the same type.
9
+ Note that an empty string is also considered valid.
10
+
11
+ Example 1:
12
+ Input: s = "()"
13
+ Output: true
14
+ Example 2:
15
+ Input: s = "()[]{}"
16
+ Output: true
17
+ Example 3:
18
+ Input: s = "(]"
19
+ Output: false
20
+ '''
21
+ # Brute Force Approach
22
+ # Time Complexity: O(n^2)
23
+ # Space Complexity: O(n)
24
+
25
+ '''
26
+ In the brute force approach, we can use a while loop to check if there are any pairs of brackets that can be removed from the string.
27
+ We can keep removing pairs of brackets until no more pairs can be removed.
28
+ If the string becomes empty, it means all brackets were matched and we return True. If there are still brackets left in the string, we return False.
29
+ '''
30
+
31
+ class Solution :
32
+ def isValid (self , s ):
33
+ while "()" in s or "[]" in s or "{}" in s :
34
+ s = s .replace ("()" , "" )
35
+ s = s .replace ("[]" , "" )
36
+ s = s .replace ("{}" , "" )
37
+
38
+ return s == ""
39
+
40
+ # Stack Approach
41
+ # Time Complexity: O(n), where n is the length of the string.
42
+ # Space Complexity: O(n), in the worst case, we need to store all opening brackets in the stack.
43
+
44
+ '''
45
+ Below is the code to solve the problem:
46
+ 1. Create a stack to keep track of the opening brackets.
47
+ 2. Create a mapping of opening brackets to closing brackets.
48
+ 3. Iterate through each character in the string:
49
+ - If the character is an opening bracket, push it onto the stack.
50
+ - If the character is a closing bracket, check if the stack is empty or if the top of the stack does not match the corresponding opening bracket.
51
+ If either condition is true, return False.
52
+ - If the character is a closing bracket and the stack is not empty, pop the top of the stack.
53
+ 4. After iterating through the string, check if the stack is empty. If it is, return True; otherwise, return False.
54
+ '''
55
+ class Solution :
56
+ def isValid (self , s ):
57
+ stack = []
58
+ mapp = {"(" :")" ,"[" :"]" ,"{" :"}" }
59
+ for ch in s :
60
+ if ch in mapp :
61
+ stack .append (ch )
62
+ elif ch in mapp .values ():
63
+ if not stack or mapp [stack .pop ()]!= ch :
64
+ return False
65
+ return True if not stack else False
66
+
67
+
68
+ class Solution :
69
+ def isValid (self , s ):
70
+ stack = []
71
+ mapp = { ")" : "(" , "]" : "[" , "}" : "{" }
72
+
73
+ for c in s :
74
+ if c in mapp :
75
+ if stack and stack [- 1 ] == mapp [c ]:
76
+ stack .pop ()
77
+ else :
78
+ return False
79
+ else :
80
+ stack .append (c )
81
+
82
+ return True if not stack else False
83
+
84
+ obj = Solution ()
85
+ s = "([{}])"
86
+ print (obj .isValid (s )) # Output: True
0 commit comments