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.
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., 3
Input:
cars bikes arcs steer
Output: 2
Naive approach is to generate all possible subset and iterate from largest size of subset containing all string having same size and anagram of each others. Time complexity of this approach is O( ) where n and m are the size of array and length of string respectively.Efficient approach is to use hashing and sorting. Sort all characters of string and store the hash value(sorted string) in map(unordered_map in C++ and HashMap in java ). At last check which one is the frequency sorted word with the highest number of occurrence.
C++
#include <bits/stdc++.h>
using
namespace
std;
int
largestAnagramSet(string arr[],
int
n)
{
int
maxSize = 0;
unordered_map<string,
int
> count;
for
(
int
i = 0; i < n; ++i) {
sort(arr[i].begin(), arr[i].end());
count[arr[i]] += 1;
maxSize = max(maxSize, count[arr[i]]);
}
return
maxSize;
}
int
main()
{
string arr[] = {
"ant"
,
"magenta"
,
"magnate"
,
"tan"
,
"gnamate"
};
int
n =
sizeof
(arr) /
sizeof
(arr[0]);
cout << largestAnagramSet(arr, n) <<
"\n"
;
string arr1[] = {
"cars"
,
"bikes"
,
"arcs"
,
"steer"
};
n =
sizeof
(arr1) /
sizeof
(arr[0]);
cout << largestAnagramSet(arr1, n);
return
0;
}
Java
import
java.util.*;
class
GFG
{
static
int
largestAnagramSet(String arr[],
int
n)
{
int
maxSize =
0
;
HashMap<String, Integer> count =
new
HashMap<>();
for
(
int
i =
0
; i < n; ++i)
{
char
temp[] = arr[i].toCharArray();
Arrays.sort(temp);
arr[i] =
new
String(temp);
if
(count.containsKey(arr[i]))
{
count.put(arr[i], count.get(arr[i]) +
1
);
}
else
{
count.put(arr[i],
1
);
}
maxSize = Math.max(maxSize, count.get(arr[i]));
}
return
maxSize;
}
public
static
void
main(String[] args)
{
String arr[] = {
"ant"
,
"magenta"
,
"magnate"
,
"tan"
,
"gnamate"
};
int
n = arr.length;
System.out.println(largestAnagramSet(arr, n));
String arr1[] = {
"cars"
,
"bikes"
,
"arcs"
,
"steer"
};
n = arr1.length;
System.out.println(largestAnagramSet(arr1, n));
}
}
Python3
def
largestAnagramSet(arr, n) :
maxSize
=
0
count
=
{}
for
i
in
range
(n) :
arr[i]
=
''.join(
sorted
(arr[i]))
if
arr[i]
in
count :
count[arr[i]]
+
=
1
else
:
count[arr[i]]
=
1
maxSize
=
max
(maxSize, count[arr[i]])
return
maxSize
arr
=
[
"ant"
,
"magenta"
,
"magnate"
,
"tan"
,
"gnamate"
]
n
=
len
(arr)
print
(largestAnagramSet(arr, n))
arr1
=
[
"cars"
,
"bikes"
,
"arcs"
,
"steer"
]
n
=
len
(arr1)
print
(largestAnagramSet(arr1, n))
C#
using
System;
using
System.Collections.Generic;
class
GFG
{
static
int
largestAnagramSet(String []arr,
int
n)
{
int
maxSize = 0;
Dictionary<String,
int
> count =
new
Dictionary<String,
int
>();
for
(
int
i = 0; i < n; ++i)
{
char
[]temp = arr[i].ToCharArray();
Array.Sort(temp);
arr[i] =
new
String(temp);
if
(count.ContainsKey(arr[i]))
{
count[arr[i]] = count[arr[i]] + 1;
}
else
{
count.Add(arr[i], 1);
}
maxSize = Math.Max(maxSize, count[arr[i]]);
}
return
maxSize;
}
public
static
void
Main(String[] args)
{
String []arr = {
"ant"
,
"magenta"
,
"magnate"
,
"tan"
,
"gnamate"
};
int
n = arr.Length;
Console.WriteLine(largestAnagramSet(arr, n));
String []arr1 = {
"cars"
,
"bikes"
,
"arcs"
,
"steer"
};
n = arr1.Length;
Console.WriteLine(largestAnagramSet(arr1, n));
}
}
Javascript
<script>
function
largestAnagramSet(arr, n)
{
var
maxSize = 0;
var
count =
new
Map();
for
(
var
i = 0; i < n; ++i)
{
var
temp = arr[i].split(
''
);
temp.sort();
arr[i] = temp.join(
''
);
if
(count.has(arr[i]))
{
count.set(arr[i], count.get(arr[i])+1);
}
else
{
count.set(arr[i], 1);
}
maxSize = Math.max(maxSize, count.get(arr[i]));
}
return
maxSize;
}
var
arr = [
"ant"
,
"magenta"
,
"magnate"
,
"tan"
,
"gnamate"
];
var
n = arr.length;
document.write(largestAnagramSet(arr, n) +
"<br>"
);
var
arr1 = [
"cars"
,
"bikes"
,
"arcs"
,
"steer"
];
n = arr1.length;
document.write(largestAnagramSet(arr1, n));
</script>
Output:
3
2
Time complexity: O( ) where m is maximum size among all of the strings Auxiliary space: O(n + m)Best approach is to store the frequency array of each word. In this, we just need to iterate over the characters of the words and increment the frequency of current letter. At last, increment the count of only identical frequency array[] and take the maximum among them. This approach is best only when length of strings are maximum in comparison to the array size .
cpp
#include <bits/stdc++.h>
using
namespace
std;
int
largestAnagramSet(string arr[],
int
n)
{
int
maxSize = 0;
map<vector<
int
>,
int
> count;
for
(
int
i = 0; i < n; ++i) {
vector<
int
> freq(26);
for
(
char
ch : arr[i])
freq[ch -
'a'
] += 1;
count[freq] += 1;
maxSize = max(maxSize, count[freq]);
}
return
maxSize;
}
int
main()
{
string arr[] = {
"ant"
,
"magenta"
,
"magnate"
,
"tan"
,
"gnamate"
};
int
n =
sizeof
(arr) /
sizeof
(arr[0]);
cout << largestAnagramSet(arr, n) <<
"\n"
;
string arr1[] = {
"cars"
,
"bikes"
,
"arcs"
,
"steer"
};
n =
sizeof
(arr1) /
sizeof
(arr[0]);
cout << largestAnagramSet(arr1, n);
return
0;
}
Java
import
java.util.*;
public
class
Main {
static
int
largestAnagramSet(String arr[],
int
n) {
int
maxSize =
0
;
Map<List<Integer>, Integer> count =
new
HashMap<>();
for
(
int
i =
0
; i < n; ++i) {
List<Integer> freq =
new
ArrayList<>(Collections.nCopies(
26
,
0
));
for
(
char
ch : arr[i].toCharArray())
freq.set(ch -
'a'
, freq.get(ch -
'a'
) +
1
);
count.put(freq, count.getOrDefault(freq,
0
) +
1
);
maxSize = Math.max(maxSize, count.get(freq));
}
return
maxSize;
}
public
static
void
main(String[] args) {
String arr[] = {
"ant"
,
"magenta"
,
"magnate"
,
"tan"
,
"gnamate"
};
int
n = arr.length;
System.out.println(largestAnagramSet(arr, n));
String arr1[] = {
"cars"
,
"bikes"
,
"arcs"
,
"steer"
};
n = arr1.length;
System.out.println(largestAnagramSet(arr1, n));
}
}
Python3
def
largestAnagramSet(arr, n):
maxSize
=
0
count
=
{}
for
i
in
range
(n):
freq
=
[
0
for
i
in
range
(
26
)]
for
ch
in
arr[i]:
freq[
ord
(ch)
-
ord
(
'a'
)]
+
=
1
temp
=
"".join(
str
(i)
for
i
in
freq)
if
temp
not
in
count:
count[temp]
=
1
else
:
count[temp]
+
=
1
maxSize
=
max
(maxSize, count[temp])
return
maxSize
arr
=
[
"ant"
,
"magenta"
,
"magnate"
,
"tan"
,
"gnamate"
]
n
=
len
(arr)
print
(largestAnagramSet(arr, n))
arr1
=
[
"cars"
,
"bikes"
,
"arcs"
,
"steer"
]
n
=
len
(arr1)
print
(largestAnagramSet(arr1, n))
C#
using
System;
using
System.Collections.Generic;
using
System.Linq;
class
Program
{
static
int
largestAnagramSet(
string
[] arr,
int
n)
{
int
maxSize = 0;
Dictionary<
string
, List<
string
>> count =
new
Dictionary<
string
, List<
string
>>();
for
(
int
i = 0; i < n; ++i)
{
char
[] chArr = arr[i].ToCharArray();
Array.Sort(chArr);
string
sortedStr =
new
string
(chArr);
if
(!count.ContainsKey(sortedStr))
count.Add(sortedStr,
new
List<
string
>());
count[sortedStr].Add(arr[i]);
maxSize = Math.Max(maxSize, count[sortedStr].Count);
}
return
maxSize;
}
static
void
Main(
string
[] args)
{
string
[] arr = {
"ant"
,
"magenta"
,
"magnate"
,
"tan"
,
"gnamate"
};
int
n = arr.Length;
Console.WriteLine(largestAnagramSet(arr, n));
string
[] arr1 = {
"cars"
,
"bikes"
,
"arcs"
,
"steer"
};
n = arr1.Length;
Console.WriteLine(largestAnagramSet(arr1, n));
}
}
Javascript
<script>
function
largestAnagramSet(arr, n){
let maxSize = 0
let count =
new
Map()
for
(let i = 0; i < n; i++){
let freq=
new
Array(26).fill(0)
for
(let ch of arr[i])
freq[ch.charCodeAt(0) -
'a'
.charCodeAt(0)] += 1
let temp = freq.join(
''
)
if
(count.has(temp) ==
false
)
count.set(temp,1)
else
count.set(temp, count.get(temp)+ 1)
maxSize = Math.max(maxSize, count.get(temp))
}
return
maxSize
}
let arr = [
"ant"
,
"magenta"
,
"magnate"
,
"tan"
,
"gnamate"
]
let n = arr.length
document.write(largestAnagramSet(arr, n),
"</br>"
)
let arr1 = [
"cars"
,
"bikes"
,
"arcs"
,
"steer"
]
n = arr1.length
document.write(largestAnagramSet(arr1, n),
"</br>"
)
</script>
Output
3
2
Time complexity: O( ) where m is maximum size among all of the strings Auxiliary space: O(n + m)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!