forked from super30admin/Array-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidParanthesis Variations.py
More file actions
55 lines (52 loc) · 1.66 KB
/
ValidParanthesis Variations.py
File metadata and controls
55 lines (52 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
ALL THE FAULT LOCATIONS OF PARANTHESIS IF THE STRING HAS ALPHABETS
"""
class Solution:
def minRemoveToMakeValid(self, s: str) -> str:
stack = [] ###On coz of stack
dictionary = {")": "(", "}": "{", "]": "["}
rep = []
flag = True
# faults=[]
for index, paranthesis in enumerate(s):
if paranthesis in dictionary.values():
stack.append((paranthesis, index))
elif not paranthesis.isalpha() and stack and dictionary[paranthesis] == stack[-1][0]:
stack.pop()
else:
if paranthesis.isalpha():
continue
rep.append((index, paranthesis))
flag = False
if stack == [] and flag == True:
print(flag)
print("rep is", rep)
print("stack is", stack)
print(rep + stack)
"""
ALL THE FAULT LOCATIONS OF PARANTHESIS IF THE STRING DOES NOT HAVE ALPHABETS
"""
class Solution:
def isValid(self, s: str) -> bool:
"""
Tc: O(n)
SC: O(n)
"""
stack=[] ###On coz of stack
dictionary={")":"(", "}":"{", "]":"["}
rep=[]
flag=True
# faults=[]
for index, paranthesis in enumerate(s):
if paranthesis in dictionary.values():
stack.append((paranthesis, index))
elif stack and dictionary[paranthesis]==stack[-1][0]:
stack.pop()
else:
# print("we here")
rep.append((index, paranthesis))
flag=False
if stack==[] and flag==True:
return flag
print(rep+stack)
return False