给定一个小写数组。我们的任务是找到最大的字符串子集的大小,该子集是彼此的字谜。字符串的字谜表示,如果第二个字符串只是第一个字符串的重新排列,则一个字符串就是另一个字符串的类似字母。在这里,我们可以使用python usingCounter()
方法快速解决此问题。
例如,字符串“ python”和“ typhon”是字谜。
Step 1: Split input string separated by space into words. Step 2: sort each string in given list of strings Step 3: now create a dictionary using a counter method which will have strings as key and their Frequencies as value. Step 4: get maximum value of frequency using max function.
# Function to find the size of largest subset # of anagram words from collections import Counter def largestana(str1): # split input string separated by space str1 = str1.split(" ") # sort each string in given list of strings for i in range(0,len(str1)): str1[i]=''.join(sorted(str1[i])) # now create a dictionary using the counter method # which will have strings as key and their # frequencies as the value newstr1 = Counter(str1) # get maximum value of frequency print ("The Size Of largest subset of Anangram word is ::>",max(newstr1.values())) # Driver program if __name__ == "__main__": str1 = input("Enter the string ::>") largestana(str1)
输出结果
Enter the string ::>qwe ewq rty ytr ytr ytr The Size Of largest subset of Anangram word is ::> 4