Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

How to check for Anagrams in python

from collections import Counter
def is_anagram(s1, s2):
  return Counter(s1) == Counter(s2)

s1 = 'listen'
s2 = 'silent'
s3 = 'runner'
s4 = 'neuron'

print(''listen' is an anagram of 'silent' -> {}'.format(is_anagram(s1, s2)))
print(''runner' is an anagram of 'neuron' -> {}'.format(is_anagram(s3, s4)))


# Output

# 'listen' an anagram of 'silent' -> True
# 'runner' an anagram of 'neuron' -> False
Source by www.kdnuggets.com #
 
PREVIOUS NEXT
Tagged: #How #check #Anagrams #python
ADD COMMENT
Topic
Name
1+8 =