-
-
Notifications
You must be signed in to change notification settings - Fork 45.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove_Invalid_Parenthesis.py #12340
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
def isParenthesis(c): | ||
return (c == "(") or (c == ")") | ||
Check failure on line 2 in backtracking/remove_invalid_parenthesis.py GitHub Actions / ruffRuff (PLR1714)
|
||
|
||
|
||
def isValidString(str): | ||
Check failure on line 5 in backtracking/remove_invalid_parenthesis.py GitHub Actions / ruffRuff (N802)
|
||
cnt = 0 | ||
for i in range(len(str)): | ||
if str[i] == "(": | ||
cnt += 1 | ||
elif str[i] == ")": | ||
cnt -= 1 | ||
if cnt < 0: | ||
return False | ||
return cnt == 0 | ||
|
||
|
||
# method to remove invalid parenthesis | ||
def removeInvalidParenthesis(str): | ||
Check failure on line 18 in backtracking/remove_invalid_parenthesis.py GitHub Actions / ruffRuff (N802)
|
||
if len(str) == 0: | ||
return | ||
|
||
visit = set() | ||
|
||
q = [] | ||
temp = 0 | ||
level = 0 | ||
|
||
q.append(str) | ||
visit.add(str) | ||
while len(q): | ||
str = q[0] | ||
q.pop(0) | ||
if isValidString(str): | ||
print(str) | ||
|
||
level = True | ||
if level: | ||
continue | ||
for i in range(len(str)): | ||
if not isParenthesis(str[i]): | ||
continue | ||
|
||
temp = str[0:i] + str[i + 1 :] | ||
if temp not in visit: | ||
q.append(temp) | ||
visit.add(temp) | ||
|
||
|
||
expression = "()())()" | ||
removeInvalidParenthesis(expression) | ||
expression = "()v)" | ||
removeInvalidParenthesis(expression) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try running pre --commit [ More on Contribution.md ]