Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Cracking Coding Interview - Check String Permutation

def is_permutation(string_a, string_b)
    hash_a = {}
    hash_b = {}
    string_a.each_char do |char|
        if hash_a.key?(char)
            hash_a[char] += 1
        else
            hash_a[char] = 1
        end
    end
    string_b.each_char do |char|
        if hash_b.key?(char)
            hash_b[char] += 1
        else
            hash_b[char] = 1
        end
    end
    hash_a == hash_b
end


p is_permutation('abc', 'cba') #true

p is_permutation('abc', 'cbb') #false


p is_permutation('mortaal', 'arotmal') #true

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.