একটি ছোট হাতের অ্যারে দেওয়া. আমাদের কাজ হল স্ট্রিংয়ের বৃহত্তম উপসেটের আকার খুঁজে বের করা যা একে অপরের একটি অ্যানাগ্রাম। স্ট্রিংয়ের অ্যানাগ্রাম মানে একটি স্ট্রিং অন্যটির একটি অ্যানাগ্রাম যদি দ্বিতীয়টি কেবল প্রথমটির পুনর্বিন্যাস হয়। এখানে আমরা কাউন্টার() পদ্ধতি ব্যবহার করে পাইথনে এই সমস্যাটি দ্রুত সমাধান করতে পারি।
উদাহরণস্বরূপ, 'পাইথন' এবং 'টাইফন' স্ট্রিংগুলি অ্যানাগ্রাম।
অ্যালগরিদম
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