view julia/strings.jl @ 3:c1db337c15be

Reorganize files
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 14 Feb 2023 21:12:06 +0100
parents
children 203bb5bbeea5
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