DataScience/LeetCode

[Easy] Valid Parentheses

Grace 2022. 11. 2. 09:06

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.
  3. Every close bracket has a corresponding open bracket of the same type.

 

Example 1:

Input: s = "()"
Output: true

Example 2:

Input: s = "()[]{}"
Output: true

Example 3:

Input: s = "(]"
Output: false

 

Constraints:

  • 1 <= s.length <= 104
  • s consists of parentheses only '()[]{}'.
var isValid = function(s) {
  function pop(s){
      if(!s) return true
      if(s.includes("()") || s.includes("[]") || s.includes("{}")) {
        s = s.replaceAll("()", "")
        s = s.replaceAll("[]", "")
        s = s.replaceAll("{}", "")
      } else {
          return false
      }
      return pop(s)
    }
  return pop(s)
};

'DataScience > LeetCode' 카테고리의 다른 글

[Easy] Sqrt(x)  (0) 2022.11.17
[Easy] Plus One  (0) 2022.11.16
[Easy] Roman to Integer  (0) 2022.10.12
[Easy] Palindrome Number  (0) 2022.10.11
[Easy] Two Sum  (0) 2022.10.06