Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #method 1
- class Solution:
- def minAddToMakeValid(self, s: str) -> int:
- ans=0
- op,cl=0,0
- for i in s:
- if i=='(':
- op+=1
- else:
- cl+=1
- if op<cl:
- ans+=1
- cl-=1
- ans+=abs(cl-op)
- return ans
- #method 2
- class Solution:
- def minAddToMakeValid(self, s: str) -> int:
- st=[]
- for i in s:
- if i=='(':
- st.append(i)
- else:
- if st and st[-1]=='(':
- st.pop()
- else:
- st.append(i)
- return len(st)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement