Iam_Sandeep

1541. Minimum Insertions to Balance a Parentheses String

Jul 13th, 2022
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. '''
  2. Intuition
  3.  
  4. Similar to 921. Minimum Add to Make Parentheses Valid.
  5. Just need to pay attention:
  6. left parenthesis '(' must have a corresponding two consecutive right parenthesis '))'.
  7.  
  8. Explanation
  9.  
  10. res represents the number of left/right parentheses already added.
  11. right represents the number of right parentheses needed.
  12.  
  13. 1) case )
  14. If we meet a right parentheses , right--.
  15. If right < 0, we need to add a left parentheses before it.
  16. Then we update right += 2 and res++
  17. This part is easy and normal.
  18.  
  19. 2) case (
  20. If we meet a left parentheses,
  21. we check if we have odd number ')' before.
  22. If we right, we have odd ')' before,
  23. but we want right parentheses in paires.
  24. So add one ')' here, then update right--; res++;.
  25. Note that this part is not necessary if two consecutive right parenthesis not required.
  26.  
  27. Because we have ), we update right += 2.
  28. Note:Always right will
  29. '''
  30. #Method 1
  31. class Solution:
  32.     def minInsertions(self, s: str) -> int:
  33.         right,ans=0,0
  34.         for i in s:
  35.             if i=='(':
  36.                 if right%2==1:
  37.                     right-=1
  38.                     ans+=1
  39.                     right+=2
  40.                 else:
  41.                     right+=2
  42.             else:
  43.                 right=right-1
  44.                 if right<0:
  45.                     ans+=1
  46.                     right+=2
  47.         return right+ ans
  48. #Method 2
  49. class Solution:
  50.     def minInsertions(self, s: str) -> int:
  51.         st=[]
  52.         ans=0
  53.         for i in s:
  54.             if i=='(':
  55.                 if not st:
  56.                     st.append(i)
  57.                 elif st[-1]=='(':
  58.                     st.append(i)
  59.                 '''
  60.                We will make sure that there will be surely '()' in top part of stack.
  61.                So two pops needed
  62.                '''
  63.                 elif st[-1]==')':
  64.                     ans+=1
  65.                     st.pop()
  66.                     st.pop()
  67.                     st.append(i)
  68.             elif i==')':
  69.                 '''
  70.                If stack is empty add extra '(' so that always () exists in top of stack
  71.                '''
  72.                 if not st:
  73.                     st.append('(')
  74.                     ans+=1
  75.                     st.append(i)
  76.                 elif st[-1]=='(':
  77.                     st.append(i)
  78.                 else:
  79.                     st.pop()
  80.                     st.pop()
  81.         if not st:return ans
  82.         else:#If remaining stack is not empty
  83.             while st:
  84.                 if st[-1]=='(':
  85.                     ans+=2
  86.                     st.pop()
  87.                 elif st[-1]==')':
  88.                     ans+=1
  89.                     st.pop()
  90.                     st.pop()
  91.         return ans
  92.  
Advertisement
Add Comment
Please, Sign In to add comment