Created
September 20, 2021 09:51
-
-
Save artyomlagun/04863ff32f4946d1e3b2e5795c1f9b74 to your computer and use it in GitHub Desktop.
Anagram on Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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