Skip to content

Instantly share code, notes, and snippets.

@gokhanakkurt
Created October 18, 2019 08:57
Show Gist options
  • Save gokhanakkurt/b41a51474bcc18c70bfcfcd02ad01ac0 to your computer and use it in GitHub Desktop.
Save gokhanakkurt/b41a51474bcc18c70bfcfcd02ad01ac0 to your computer and use it in GitHub Desktop.
Determine whether given string of parentheses is properly nested in Swift
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