Skip to content

Instantly share code, notes, and snippets.

@reyandrey
Created August 12, 2020 21:13
Show Gist options
  • Save reyandrey/ba271df713efcb2ca657b7a030d731fa to your computer and use it in GitHub Desktop.
Save reyandrey/ba271df713efcb2ca657b7a030d731fa to your computer and use it in GitHub Desktop.
Another task
import Foundation
/*
(())()
(()
)((())
*/
func isValid(_ str: String) -> Bool {
var count = 0
for c in str {
switch c {
case "(": count += 1
case ")": count -= 1
default: count = -1; break
}
guard count >= 0 else { break }
}
return count == 0
}
print(isValid("(())()"))
print(isValid("(()"))
print(isValid(")((())"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment