simple-exercises/ruby-exercises/easy-6.rb
# Anagrams are two words with the exact same letters.
# Write a function that take two strings and returns true if they are anagrams
# and false if they are not.
Anagrams are two words with the exact same letters.
Write a function that take two strings and returns true if they are anagrams
and false if they are not.
Anagrams are two words with the exact same letters . Write a function that take two strings and returns true if they are anagrams and false if they are not .
def anagrams?(str1, str2)
  if str1.length != str2.length
    return false
  end
  chars1 = str1.split("")
  chars2 = str2.split("")
  c1counts = Hash.new(0)
  c2counts = Hash.new(0)
  (0...str1.length).each do |i|
    c1, c2 = chars1[i], chars2[i]
    c1counts[c1] += 1
    c2counts[c2] += 1
  end
  if c1counts.count != c2counts.count
    return false
  end
  c1counts.each do |k, v|
    if c2counts[k] != v
      return false
    end
  end
  true
end
def anagrams? ( str1 , str2 ) if str1 . length != str2 . length return false end chars1 = str1 . split ( "" ) chars2 = str2 . split ( "" ) c1counts = Hash . new ( 0 ) c2counts = Hash . new ( 0 ) ( 0 ... str1 . length ) . each do | i | c1 , c2 = chars1 [ i ] , chars2 [ i ] c1counts [ c1 ] += 1 c2counts [ c2 ] += 1 end if c1counts . count != c2counts . count return false end c1counts . each do | k , v | if c2counts [ k ] != v return false end end true end
Anagrams are two words with the exact same letters.
Anagrams are two words with the exact same letters .
# make character freq table
str1
<not_specific>
str2
<not_specific>