Last active
October 19, 2019 09:54
-
-
Save praveeng1618/8a4c1048b6f0ff8e0e35d4d604e9590b to your computer and use it in GitHub Desktop.
your'e given a string of parenthesis. return minimum number of parenthesis need to be removed , in order to make string valid. "valid" means each open parenthesis has a matching closed parenthesis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
par = input() | |
list = [] | |
invalid = 0 | |
for i in range(len(par)): | |
if par[i] == '(': | |
list.append(par[i]) | |
elif par[i] == ')': | |
if len(list) == 0: | |
invalid = invalid + 1 | |
else: | |
list.pop() | |
if len(list) != 0: | |
invalid = invalid + len(list) | |
print(invalid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
()()() valid
((())) Valid
(()()) Valid
)()()) Invalid
)(()() Invalid
()())() Invalid