view strings.jl @ 1:ed34696a9d9b

Some better sorting
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 12 Feb 2023 20:12:55 +0100
parents d0c890aae379
children
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