Sha256: 0ed5f2677338ebc0df2432bf6e5e50cda64b2548ed025e071bc283c5cf9e0cb8

Contents?: true

Size: 1.11 KB

Versions: 311

Compression:

Stored size: 1.11 KB

Contents

# Anagrams are two or more words that composed of the same characters but in a different order

function is_anagram(s1::AbstractString, s2::AbstractString)
    # Disable case sensitivity
    s1 = lowercase(s1)
    s2 = lowercase(s2)

    # Similar and different-length strings are not anagrams
    if length(s1) != length(s2) || s1 == s2
        return false
    end

    # Calculate count of every character in the first string
    chr_count = Dict()
    for c in s1
        chr_count[c] = get(chr_count, c, 0) + 1
    end

    # Reduce the count by every character from the second string
    for c in s2
        t = get(chr_count, c, 0) - 1
        if t < 0
            # Got character that not exist in the first string
            return false
        else
            chr_count[c] = t
        end
    end

    # Check all counts to be zeroes
    return all(i->(i==0), values(chr_count))
end

function detect_anagrams(subject::AbstractString, candidates::AbstractArray)
    result = []
    for candidate = candidates
        if is_anagram(subject, candidate)
            push!(result, candidate)
        end
    end
    result
end

Version data entries

311 entries across 311 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.179 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.178 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.177 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.176 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.175 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.174 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.173 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.172 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.171 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.170 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.169 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.167 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.166 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.165 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.164 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.163 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.162 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.161 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.160 tracks/julia/exercises/anagram/example.jl