Mercurial > lbo > hg > juliaplay
view strings.jl @ 0:d0c890aae379
Initial commit
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 26 Nov 2022 09:23:14 +0100 |
parents | |
children | ed34696a9d9b |
line wrap: on
line source
# "(())()" -> good; # "(()" -> bad function goodparens(s::String)::Bool nesting = 0 for c in s if c == '(' nesting += 1 elseif c == ')' nesting -= 1 end if nesting < 0 return false end end if nesting != 0 false else true end end # "[()]{}" -> good; "[(])" -> bad function goodmultiparens(s::String)::Bool stack::Vector{Char} = [] for c in s if c in ['(', '[', '{'] push!(stack, c) elseif c in [')', ']', '}'] o = pop!(stack); if (c == ')' && o == '(') || (c==']' && o == '[') || (c=='}' && o == '{') continue else return false end end end length(stack) == 0 end