Advertisement
azamat_tajiyev

20. Valid Parentheses

Apr 24th, 2024 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.48 KB | None | 0 0
  1. class Solution {
  2.     func isValid(_ s: String) -> Bool {
  3.         if [")", "]", "}"].contains(s.first) { return false }
  4.         if ["(", "[", "{"].contains(s.last) { return false }
  5.         var str: String = ""
  6.         for c in s {
  7.             if str.last == "(" && c == ")" || str.last == "[" && c == "]" || str.last == "{" && c == "}" {
  8.                 str.removeLast()
  9.             } else {
  10.                 str.append(c)
  11.             }
  12.         }
  13.         return str.count == 0
  14.     }
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement