找出最大的字谜词子集的大小
给定一个包含小写字母的 n字符串数组。找到字符串的最大子集的大小,它们是彼此的字谜。一个字符串的变位词是另一个包含相同字符的字符串,只是字符的顺序可以不同。例如,“abcd”和“dabc”是彼此的字谜。
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
天真的方法是生成所有可能的子集并从包含所有具有相同大小的字符串和彼此的字谜的最大子集的大小进行迭代。这种方法的时间复杂度是 O( ) 其中 n 和 m 分别是数组的大小和字符串的长度。
有效的方法是使用散列和排序。对字符串的所有字符进行排序,并将哈希值(排序后的字符串)存储在 map(C++ 中的 unordered_map 和Java中的 HashMap)中。最后检查哪个是出现次数最多的频率排序词。
C++
// C++ Program to find the size of
// largest subset of anagram
#include
using namespace std;
// Utility function to find size of
// largest subset of anagram
int largestAnagramSet(string arr[], int n)
{
int maxSize = 0;
unordered_map count;
for (int i = 0; i < n; ++i) {
// sort the string
sort(arr[i].begin(), arr[i].end());
// Increment the count of string
count[arr[i]] += 1;
// Compute the maximum size of string
maxSize = max(maxSize, count[arr[i]]);
}
return maxSize;
}
// Driver code
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
// Java Program to find the size of
// largest subset of anagram
import java.util.*;
class GFG
{
// Utility function to find size of
// largest subset of anagram
static int largestAnagramSet(String arr[], int n)
{
int maxSize = 0;
HashMap count = new HashMap<>();
for (int i = 0; i < n; ++i)
{
// sort the String
char temp[] = arr[i].toCharArray();
Arrays.sort(temp);
arr[i] = new String(temp);
// Increment the count of String
if(count.containsKey(arr[i]))
{
count.put(arr[i], count.get(arr[i]) + 1);
}
else
{
count.put(arr[i], 1);
}
// Compute the maximum size of String
maxSize = Math.max(maxSize, count.get(arr[i]));
}
return maxSize;
}
// Driver code
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));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 Program to find the size of
# largest subset of anagram
# Utility function to find size of
# largest subset of anagram
def largestAnagramSet(arr, n) :
maxSize = 0
count = {}
for i in range(n) :
# sort the string
arr[i] = ''.join(sorted(arr[i]))
# Increment the count of string
if arr[i] in count :
count[arr[i]] += 1
else :
count[arr[i]] = 1
# Compute the maximum size of string
maxSize = max(maxSize, count[arr[i]])
return maxSize
# Driver code
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))
# This code is contributed by divyeshrabadiya072019
C#
// C# Program to find the size of
// largest subset of anagram
using System;
using System.Collections.Generic;
class GFG
{
// Utility function to find size of
// largest subset of anagram
static int largestAnagramSet(String []arr, int n)
{
int maxSize = 0;
Dictionary count = new Dictionary();
for (int i = 0; i < n; ++i)
{
// sort the String
char []temp = arr[i].ToCharArray();
Array.Sort(temp);
arr[i] = new String(temp);
// Increment the count of String
if(count.ContainsKey(arr[i]))
{
count[arr[i]] = count[arr[i]] + 1;
}
else
{
count.Add(arr[i], 1);
}
// Compute the maximum size of String
maxSize = Math.Max(maxSize, count[arr[i]]);
}
return maxSize;
}
// Driver code
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));
}
}
// This code is contributed by Rajput-Ji
Javascript
cpp
// C++ Program to find the size of
// largest subset of anagram
#include
using namespace std;
// Utility function to find size of
// largest subset of anagram
int largestAnagramSet(string arr[], int n)
{
int maxSize = 0;
// Initialize map<> of vector array
map, int> count;
for (int i = 0; i < n; ++i) {
// Vector array to store
// frequency of element
vector freq(26);
for (char ch : arr[i])
freq[ch - 'a'] += 1;
// Increment the count of
// frequency array in map<>
count[freq] += 1;
// Compute the maximum size
maxSize = max(maxSize, count[freq]);
}
return maxSize;
}
// Driver code
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;
}
Python3
# Python Program to find the size of
# largest subset of anagram
# Utility function to find size of
# largest subset of anagram
def largestAnagramSet(arr, n):
maxSize = 0
# Initialize dictionary of array
count = {}
for i in range(n):
# list to store
# frequency of element
freq=[0 for i in range(26)]
for ch in arr[i]:
freq[ord(ch) - ord('a')] += 1
# Increment the count of
# frequency array in dictionary
temp = "".join(str(i) for i in freq)
if temp not in count:
count[temp] = 1
else:
count[temp] += 1
# Compute the maximum size
maxSize = max(maxSize, count[temp])
return maxSize
# Driver code
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))
# This code is contributed by rag2127
输出:
3
2
时间复杂度: O( ) 其中 m 是所有字符串中的最大大小
辅助空间: O(n + m)
最好的方法是存储每个单词的频率数组。在这种情况下,我们只需要遍历单词的字符并增加当前字母的频率。最后,只增加相同频率数组[]的计数,并取其中的最大值。只有当字符串的长度与数组大小相比最大时,这种方法才是最好的。
cpp
// C++ Program to find the size of
// largest subset of anagram
#include
using namespace std;
// Utility function to find size of
// largest subset of anagram
int largestAnagramSet(string arr[], int n)
{
int maxSize = 0;
// Initialize map<> of vector array
map, int> count;
for (int i = 0; i < n; ++i) {
// Vector array to store
// frequency of element
vector freq(26);
for (char ch : arr[i])
freq[ch - 'a'] += 1;
// Increment the count of
// frequency array in map<>
count[freq] += 1;
// Compute the maximum size
maxSize = max(maxSize, count[freq]);
}
return maxSize;
}
// Driver code
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;
}
Python3
# Python Program to find the size of
# largest subset of anagram
# Utility function to find size of
# largest subset of anagram
def largestAnagramSet(arr, n):
maxSize = 0
# Initialize dictionary of array
count = {}
for i in range(n):
# list to store
# frequency of element
freq=[0 for i in range(26)]
for ch in arr[i]:
freq[ord(ch) - ord('a')] += 1
# Increment the count of
# frequency array in dictionary
temp = "".join(str(i) for i in freq)
if temp not in count:
count[temp] = 1
else:
count[temp] += 1
# Compute the maximum size
maxSize = max(maxSize, count[temp])
return maxSize
# Driver code
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))
# This code is contributed by rag2127
Output
3
2
时间复杂度: O( ) 其中 m 是所有字符串中的最大大小
辅助空间: O(n + m)