लोअरकेस सरणी को देखते हुए। हमारा कार्य स्ट्रिंग के सबसे बड़े उपसमुच्चय का आकार ज्ञात करना है जो एक दूसरे का विपर्ययण है। स्ट्रिंग के विपर्यय का अर्थ है कि एक स्ट्रिंग दूसरे का विपर्यय है यदि दूसरा केवल पहले की पुनर्व्यवस्था है। यहां हम काउंटर () पद्धति का उपयोग करके अजगर में इस समस्या को जल्दी से हल कर सकते हैं।
उदाहरण के लिए, स्ट्रिंग्स 'पायथन' और 'टाइफॉन' विपर्ययण हैं।
एल्गोरिदम
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