Skip to content

Instantly share code, notes, and snippets.

@artyomlagun
Created September 20, 2021 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artyomlagun/04863ff32f4946d1e3b2e5795c1f9b74 to your computer and use it in GitHub Desktop.
Save artyomlagun/04863ff32f4946d1e3b2e5795c1f9b74 to your computer and use it in GitHub Desktop.
Anagram on Ruby
def is_anagram?(first, second)
first = first.split('').sort.join # get sorted string from words characters
second = second.split('').sort.join
matches = first.length == second.length # check match by length
return if !matches # return if words have different length
first.length.times do |i| # check every character in sorted string
matches = first[i] == second[i]
break if !matches # break loop if characters don't equal
end
matches
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment