Created
September 26, 2013 10:12
-
-
Save dinks/6712229 to your computer and use it in GitHub Desktop.
Anagram
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
# A string is a palindrome if it has exactly the same sequence of characters when read left-to-right as it has when read right-to-left. For example, the following strings are palindromes: | |
# "kayak", | |
# "codilitytilidoc", | |
# "neveroddoreven". | |
# A string A is an anagram of a string B if A can be obtained from B by rearranging the characters. For example, in each of the following pairs one string is an anagram of the other: | |
# | |
def anagram(s) | |
temp = 0 | |
s.each_byte do |letter| | |
temp ^= 1 << (letter - 97) | |
end | |
return true if temp == 0 || (temp & (temp-1)) == 0 | |
false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment