Skip to content
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

Adding interview question "redundant brackets" #115

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Redundant Brackets
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from sys import stdin

def checkRedundantBrackets(expression) :
# Your code goes here

# create a stack of characters
st = []

# Iterate through the given expression
for ch in expression:

# if current character is close
# parenthesis ')'
if (ch == ')'):
top = st[-1]
st.pop()

# If immediate pop have open parenthesis
# '(' duplicate brackets found
flag = True

while (top != '('):

# Check for operators in expression
if (top == '+' or top == '-' or
top == '*' or top == '/'):
flag = False

# Fetch top element of stack
top = st[-1]
st.pop()

# If operators not found
if (flag == True):
return True

else:
st.append(ch) # append open parenthesis '(',
# operators and operands to stack
return False