주어진 소문자 배열. 우리의 임무는 서로의 아나그램인 문자열의 가장 큰 부분 집합의 크기를 찾는 것입니다. 문자열의 아나그램은 두 번째가 단순히 첫 번째 문자열의 재배열인 경우 한 문자열이 다른 문자열의 아나그램임을 의미합니다. 여기서 Counter() 메서드를 사용하여 파이썬에서 이 문제를 빠르게 해결할 수 있습니다.
예를 들어 '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 dictionary using 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 dictionary using counter method
# which will have strings as key and their
# frequencies as 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