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.119 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.118 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.117 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.116 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.115 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.114 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.113 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.111 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.110 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.109 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.108 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.107 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.106 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.105 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.104 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.103 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.102 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.101 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.100 tracks/julia/exercises/anagram/example.jl
trackler-2.2.1.99 tracks/julia/exercises/anagram/example.jl