Two words form a "metathesis pair" if you can transform one into the other by swapping two letters; for example, "converse" and "conserve". Write a function metathesis(word1, word2) to check if the two words can form a metathesis pair. The function will return a Boolean value (True or False). python code
def metathesis(word1, word2): are_anagrams = True if len(word1)==len(word2): for x in word1: # Checking count of character x is same if word1.count(x) != word2.count(x): are_anagrams = False for x in word2: # Checking count of character x is same if word1.count(x) != word2.count(x): are_anagrams = False if are_anagrams: count = 0 for i in range(len(word1)): if word1[i]!=word2[i]: count += 1 return count==2 return False # Testing print(metathesis("converse","conserve"))
True
Get Answers For Free
Most questions answered within 1 hours.