Skip to content

Instantly share code, notes, and snippets.

@praveeng1618
Last active October 19, 2019 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save praveeng1618/8a4c1048b6f0ff8e0e35d4d604e9590b to your computer and use it in GitHub Desktop.
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
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)
@praveeng1618
Copy link
Author

()()() valid
((())) Valid
(()()) Valid
)()()) Invalid
)(()() Invalid
()())() Invalid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment