InfyTQ 2019 : Find the position from where the parenthesis is not balanced

Given a string str consisting of parenthesis from [ “(” , “)” , “{” , “}” , “[” , “]” ].
If the String is perfectly balanced return 0 else return the index(starting from 1)at which the nesting is found to be wrong.

Examples:

Input : str = “{[()]}[]”
Output : 0

Input : str = “[{]()}”
Output : 3

Input : str = “}[{}]”
Output : 1

Input : str = “{([]){”
Output : 7

Approach:

  1. First we are creating the lst1 and lst2 that will have the opening and closing brackets respectively
  2. Now we will create another list lst that will work as a Stack means if any closing brackets will appear in the string then the respective opening brackets present in the lst will be deleted
  3. If any closing brackets will appear in the string and no repective opening brackets is present in the lst then this will be the bad index we will return it
  4. If we reach at the end of the string while traversing and the size of the lst is not equals to 0 we will return the len(String)+1
  5. Note : We are using python dictionary to get the respective opening brackets for a closing one.

Below is the impementation of the above approach

filter_none

edit
close

play_arrow

link
brightness_4
code

# Write Python3 code here
  
# Defining the string
string = "{[()]}[]"
  
# Storing opening braces in list lst1
lst1 =['{', '(', '[']
  
# Storing closing braces in list lst2
lst2 =['}', ')', ']']
  
# Creating an empty list lst
lst =[]
  
# Creating dictionary to map 
# closing braces to opening ones
Dict ={ ')':'(', '}':'{', ']':'['}
  
a = b = c = 0
  
# If first position of string contain 
# any closing braces return 1
if string[0in lst2:
    print(1)
       
else
    # If characters of string are opening 
    # braces then append them in a list
    for i in range(0, len(string)):
        if string[i] in lst1:
            lst.append(string[i])
            k = i + 2
        else:
            # When size of list is 0 and new closing
            # braces is encountered then print its
            # index starting from 1          
            if len(lst)== 0 and (string[i] in lst2):
                print(i + 1)
                c = 1
                break
            else:    
                # As we encounter closing braces we map 
                # them with theircorresponding opening 
                # braces using dictionary and check
                # if it is same as last opened braces 
                #(last element in list) if yes then we
                # delete that elememt from list
                if Dict[string[i]]== lst[len(lst)-1]:
                    lst.pop()
                else:
                    # Otherwise we return the index
                    # (starting from 1) at which
                    # nesting is found wrong    
                    print(i + 1)
                    a = 1
                    break
                       
    # At end if the list is empty it
    # means the string is perfectly nested               
    if len(lst)== 0 and c == 0:
        print(0)
        b = 1
           
    if a == 0 and b == 0 and c == 0:
        print(k)

chevron_right


Output:

0


My Personal Notes arrow_drop_up

here to learn something new

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.