Created
August 12, 2020 21:13
-
-
Save reyandrey/ba271df713efcb2ca657b7a030d731fa to your computer and use it in GitHub Desktop.
Another task
This file contains hidden or 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
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