[Leetcode] bracket question

whitehousechef·2024년 5월 18일

https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/description/

i immediately thought of using stack n space but there is actually constant space

def bracket_match(text):
  ans =0
  count_open = 0
  for i in text:
    if i=='(':
      count_open+=1
    else:
      if count_open==0:
        ans+=1
      else:
        count_open-=1
  return count_open+ans

0개의 댓글