Python Counter to find the size of largest subset of anagram words
Last Updated :
27 Jul, 2023
Given an array of n string containing lowercase letters. Find the size of largest subset of string which are anagram of each others.
An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other. Examples:
Input:
ant magenta magnate tan gnamate
Output: 3
Explanation
Anagram strings(1) - ant, tan
Anagram strings(2) - magenta, magnate,
gnamate
Thus, only second subset have largest
size i.e., 3Input:
cars bikes arcs steer
Output: 2
Python Counter to find the size of largest subset of anagram words
We have existing solution for this problem please refer Find the size of largest subset of anagram words link. We can solve this problem quickly in python using Counter() method. Approach is very simple,
- Split input string separated by space into words.
- As we know two strings are anagram to each other if they contain same character set. So to get all those strings together first we will sort each string in given list of strings.
- Now create a dictionary using Counter method having strings as keys and their frequencies as value.
- Check for maximum value of frequencies, that will be the largest sub-set of anagram strings.
Python3
# Function to find the size of largest subset
# of anagram words
from collections import Counter
def maxAnagramSize(input):
# split input string separated by space
input = input.split(" ")
# sort each string in given list of strings
for i in range(0,len(input)):
input[i]=''.join(sorted(input[i]))
# now create dictionary using counter method
# which will have strings as key and their
# frequencies as value
freqDict = Counter(input)
# get maximum value of frequency
print (max(freqDict.values()))
# Driver program
if __name__ == "__main__":
input = 'ant magenta magnate tan gnamate'
maxAnagramSize(input)
Output:
3
Python Counter to find the size of largest subset of anagram words Using dictionary
Approach
it uses a dictionary to group words with the same set of characters. However, instead of using a frozen set of character counts as the key, it sorts the characters in each word and uses the resulting string as the key.
Algorithm
1. Create an empty dictionary called anagram_dict.
2. Loop through each word in the input list words:
a. Sort the characters in the word and store the result as a string.
b. If the sorted string is not already in the dictionary, add it as a key and set its value to an empty list.
c. Append the original word to the list of values for the corresponding key in the dictionary.
3. Find the maximum length of the values in the dictionary.
4. Return the maximum length.
Python3
def largest_anagram_subset_size(words):
anagram_dict = {}
for word in words:
sorted_word = ''.join(sorted(word))
if sorted_word not in anagram_dict:
anagram_dict[sorted_word] = []
anagram_dict[sorted_word].append(word)
max_count = max([len(val) for val in anagram_dict.values()])
return max_count
words = ['ant', 'magenta', 'magnate', 'tan', 'gnamate']
print(largest_anagram_subset_size(words))
Time complexity: O(n * k log k) where n is the number of words and k is the maximum length of a word in the list. This is because for each word, we need to sort its characters, which takes O(k log k) time, and we do this n times for each word in the input list
Auxiliary Space: O(n * k) where n is the number of words and k is the maximum length of a word in the list. This is because we use a dictionary to store the anagram groups, and each word in the list may need to be stored in the dictionary with its sorted characters as the key. The size of each value list in the dictionary can also be up to n, the size of the input list. Therefore, the total space required is proportional to n times k.
Python Counter to find the size of largest subset of anagram words Using lambda
We need to find the size of the largest subset of anagram words in the given list of words. We can use the collections.Counter class to create a dictionary of the counts of each character in a given string. We then compare the Counter objects of each word in the list with the Counter objects of every other word in the list to determine if they are anagrams. Finally, we find the maximum count of anagrams for any word in the list to determine the size of the largest subset of anagram words.
Algorithm
1. Initialize max_anagrams to 0.
2. For each word x in the list of words:
a. Create a generator expression that maps each word y in the list of words to 1 if it is an anagram of x, and 0 otherwise.
b. Sum the resulting list of 1's and 0's to obtain the number of anagrams for x.
c. Update max_anagrams to the maximum of its current value and the number of anagrams for x.
3. Output max_anagrams.
Python3
from collections import Counter
words = ['cars', 'bikes', 'arcs', 'steer']
max_anagrams = max(
list(
map(
lambda x: sum(
map(
lambda y: Counter(y) == Counter(x),
words
)
),
words
)
),
default=0
)
print(max_anagrams)
Time complexity: O(n^2 * k), where n is the length of the list of words and k is the maximum number of distinct characters in a word.
Auxiliary Space: O(n * k), where n is the length of the list of words and k is the maximum number of distinct characters in a word.
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
DSA Tutorial - Learn Data Structures and Algorithms
DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Breadth First Search or BFS for a Graph
Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read