add new solutions

This commit is contained in:
kanna5 2025-12-23 17:59:03 +09:00
parent ccb8b5673b
commit 58527849b2
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
6 changed files with 202 additions and 0 deletions

View file

@ -0,0 +1,32 @@
package q20
func isValid(s string) bool {
stack := make([]byte, 0, len(s)/2)
for i := range len(s) {
switch s[i] {
case '(', '[', '{':
stack = append(stack, s[i])
case ')':
if len(stack) == 0 || stack[len(stack)-1] != '(' {
return false
}
stack = stack[:len(stack)-1]
case ']':
if len(stack) == 0 || stack[len(stack)-1] != '[' {
return false
}
stack = stack[:len(stack)-1]
case '}':
if len(stack) == 0 || stack[len(stack)-1] != '{' {
return false
}
stack = stack[:len(stack)-1]
}
}
return len(stack) == 0
}
var _ = isValid