leet code valid-parentheses

2024. 11. 13. 20:22boj

valid parenthess 풀이

 

string 의 길이를 돌면서 가장 최근 열린 괄호가 닫힌괄호와 같은지를 판단하고,

아니라면 false 를 return 그리고 마무리 까지 진행하다 닫히지 않은 괄호가 존재하면 false 를 리턴하고

아니라면 true를 리턴하면 풀이가 끝난다

 

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```typescript []
const types = {
'}' : '{',
')' : '(',
']' : '[',
}
function isValid(s: string): boolean {
  const temp = [];
  for (let i = 0; i < s.length; i++) {
    if (types[s[i]]) {
      if (temp.length === 0) {
        return false;
      }
      if (temp[temp.length - 1] !== types[s[i]]) {
        return false;
      }
      temp.pop();
    }
    else {
      temp.push(s[i]);
    }
  }
 
  if (temp.length > 0) {
    return false;
  }
  return true;
};
```

'boj' 카테고리의 다른 글

leet code invert binary tree  (0) 2024.11.16
valid palindrome  (0) 2024.11.14
경쟁적 전염  (0) 2023.01.30
15649 문제 해결.  (0) 2023.01.04
1181cpp 문제 해결.  (0) 2022.12.30