Advertisement
azamat_tajiyev

1614. Maximum Nesting Depth of the Parentheses

Apr 11th, 2024 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.58 KB | None | 0 0
  1. class Solution {
  2.     func maxDepth(_ s: String) -> Int {
  3.         var count = 0
  4.         var max = 0
  5.         var b = ""
  6.         for char in s {
  7.             if char == "(" {
  8.                 b.append(char)
  9.                 count += 1
  10.             } else if char == ")" {
  11.                 if max < count {
  12.                     max = count
  13.                 }
  14.                 if b.last == "(" {
  15.                     b.removeLast()
  16.                 }
  17.                 count -= 1
  18.             }
  19.         }
  20.         if b.isEmpty {
  21.             return max
  22.         }
  23.         return 0
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement