Created
October 18, 2019 08:57
-
-
Save gokhanakkurt/b41a51474bcc18c70bfcfcd02ad01ac0 to your computer and use it in GitHub Desktop.
Determine whether given string of parentheses is properly nested in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isNestedString(_ S : String) -> Bool { | |
if S.isEmpty { | |
return true | |
} | |
var stack: [Character] = [Character]() | |
for elem in S { | |
if elem == "(" { | |
stack.append(elem) | |
}else if elem == ")" { | |
guard let _ = stack.popLast() else { return false } | |
} | |
} | |
return stack.isEmpty | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment