Python program to find the size of the largest subset of anagram words


This article teaches you how to write a Python program to find the size of the largest subset of anagram words

Every character of the rearranged string or number must also be a component of another string or number in order for the condition of an anagram to exist. To put it another way, a string is said to be an anagram of another if the second is just the first string rearranged. As an illustration, the words The program and rogPrma are anagrams, as are the words Code and doCe.

Input-Output Scenarios

Lets take an input and its output scenario to find the size of the largest subset of anagram words.

Input: Facebook bookecFa Twitter ceaFkoob School 
Outpu: 3

In the given input, only 3 strings i.e. “Facebook”, “bookecFa” and “ceaFkoob” have the largest anagram size with 8 letters in it. Hence, the output is 3.

Algorithm

Following is an algorithm to find the size of the largest subset of anagram words −

  • Obtain the user's string inputs and put them in separate variables.

  • Sort both of the strings into lists using the sort() method.

  • Check the formation of anagrams between the two lists.

  • Print the outcome.

  • Exit

Example

The anagramCheck() method, which accepts two strings as an input, is declared in the below above. To sort these strings, a list of them is created. The position variable was then defined and given the value zero.

The string length and position value are compared throughout each iteration of the while loop. Each item on both lists was compared to one another and given a one-point boost in position value. The loop is terminated and returns true when the position value exceeds the length of the string; otherwise, it returns false.

def anagram(string1,string2): 
# Converting the string into lists 
   lst1 = list(string1) 
   lst2 = list(string2) 
# Sorting the list value 
   lst1.sort() 
   lst2.sort() 
   position = 0 
   matche = True

   while position < len(string1) and matche: 
      if lst1[position]==lst2[position]: 
         position = position + 1 
      else: 
         matche = False 
      return matche 
print(anagram('Coding','dingoC'))

Output

True

Using Naive approach

The nave method is to create every possible subset and then iterate starting from the largest size of the subset that contains all strings that are the same size and anagrams of one another. This method's time complexity is O((2^n)m), where n and m are the array's and the string's respective sizes.

Example

Following is an example to to find the size of the largest subset of anagram word using naive approach −

def anagram(array, n) :
   maximumSize = 0 
   count = {} 
   for i in range(s) : 
      # sorting the string array[i] = ''.join(sorted(array[i]))
      # Incrementing the count of the string 
      if array[i] in count :
         count[array[i]] += 1 
      else : 
         count[array[i]] = 1 

      # Computing the maximum size of the strings 
      maximumSize = max(maximumSize, count[array[i]]) 
   return maximumSize 

# The driver code array = [ "Facebook", "bookecFa", "Twitter", "ceaFkoob", "School" ] 
s = len(array) 
print('The largest size of anagram is :', anagram(array, s)) 
arrayA = [ "hot", "cold", "toh", "dolc", "rat", "mice"] 
s = len(arrayA) 
print('The largest size of anagram is :', anagram(arrayA, s))

Output

Following is an output of the above code −

The largest size of anagram is : 3 
The largest size of anagram is : 2

Example

The best way to proceed is to store each word's frequency array. We just iterate over the word's letters in this case and increase the frequency of the current letter. Increase the count of only identical frequency arrays [] , then select the largest value. This strategy work well only when string lengths are at their maximum in respect to array sizes.

def anagram(array, n) : 
   maximumSize = 0 
   # Initializing the dictionary of array 
   count = {} 
   for i in range(s) : 
      # list to store the frequency of element 
      frequency=[0 for i in range(28)] 
      for char in array[i]: 
         frequency[ord(char) - ord('b')] += 1 
      # Incrementing the count of the frequency array in the dictionary 
      temp = "".join(str(i) for i in frequency) 
      if temp not in count: 
         count[temp] = 1
      else: 
         count[temp] += 1 
      # Computing the maximum size 
      maximumSize = max(maximumSize, count[temp]) 
   
   return maximumSize 
# The driver code 
array = [ "Facebook", "bookecFa", "Twitter", "ceaFkoob", "School" ] 
s = len(array) 
print('The largest size of anagram is :', anagram(array, s)) 
arrayA = [ "hot", "cold", "toh", "dolc", "rat", "mice"] 
s = len(arrayA) 
print('The largest size of anagram is :', anagram(arrayA, s))

Output

Following is an output of the above code −

The largest size of anagram is : 3 
The largest size of anagram is : 2

Using counter() method

Words from the input string are separated by spaces. Each string in the list of strings is sorted. Using the counter method, establish a dictionary with strings as the key and their frequencies as the value. Utilize the max function to get the frequency's maximum value.

Example

Following is an example to to find the size of the largest subset of anagram word using counter() method −

from collections import Counter 
def anagram(string1): 
   # splitting input string separated by the space 
   string1 = string1.split(" ")

   # sorting each string in the given list of strings 
   for i in range(0,len(string1)): 
      string1[i]=''.join(sorted(string1[i])) 

# creating the dictionary using counter method having strings as key and their frequencies as values 
   newstring1 = Counter(string1)

   # getting the maximum value of the frequency 
   print ("The Size Of the largest subset of the Anangram word is ::>",max(newstring1.values())) 
      # The driver program 
   if __name__ == "__main__": 
      string1 = input("Enter any string ::>") 
anagram(string1)

Output

Following is an output of the above code −

Enter any string ::>"Facebook", "bookecFa", "Twitter", "ceaFkoob", "School" 
The Size Of the largest subset of the Anangram word is ::> 3

Updated on: 04-Apr-2023

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements