나는 이렇게 학습한다/Algorithm & SQL
Anagram Detection
daco2020
2022. 7. 21. 17:48
An anagram is the result of rearranging the letters of a word to produce a new word (see wikipedia).
Note: anagrams are case insensitive
Complete the function to return true if the two arguments given are anagrams of each other; return false otherwise.
Examples
- "foefet" is an anagram of "toffee"
- "Buckethead" is an anagram of "DeathCubeK"
Solution:
def is_anagram(test, original):
if len(test) < len(original):
test, original = original, test
arr = [i for i in test.lower()]
[arr.remove(i) for i in original.lower() if i in arr]
return not arr
Subtract a smaller string from a larger string to see if they match.
Best Practice:
def is_anagram(test, original):
return sorted(original.lower()) == sorted(test.lower())
Sorting and comparing makes it easier to check.