Created
October 31, 2018 05:56
-
-
Save yoosuf/e0edca5313d80aea41254e7907acdecd to your computer and use it in GitHub Desktop.
Simple Anagram with Ruby
This file contains hidden or 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 anagram(str1, str2) | |
if str1.nil? || str2.nil? || str1.empty? || str2.empty? | |
return false | |
end | |
demo1 = str1.split("").sort | |
demo2 = str2.split("").sort | |
test1 = noirmalise(demo1) | |
test2 = noirmalise(demo2) | |
test1.join == test2.join | |
end | |
def noirmalise (data) | |
data.join.scan(/[a-z]/) | |
end | |
# puts '===== Positive cases ======' | |
puts anagram('google', 'ggoole') | |
# puts '===== Negative cases ======' | |
puts anagram('google', 'yahoo') | |
puts anagram('', '') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment