给定一组单词,一起打印所有字谜。
例如,
Input: array = {“cat”, “dog”, “tac”, “god”, “act”}
output: cat tac act, dog god
Explanation: cat tac and act are anagrams
and dog and god are anagrams as
they have the same set of characters.
Input: array = {“abc”, “def”, “ghi”}
output: abc, def, ghi
Explanation: There are no anagrams in the array.
此处讨论了其他方法,这些帖子:
- 给出一个单词序列打印所有字谜在一起
- 给定的单词序列打印所有字谜一起设置 2
方法:这是一个使用存储键值对的 C++ 标准模板库的 HashMap 解决方案。在哈希图中,键将是排序的字符集,值将是输出字符串。当它们的字符被排序时,两个字谜将是相似的。现在,
- 将向量元素存储在 HashMap 中,键作为排序字符串。
- 如果key相同,则将该字符串与HashMap(字符串 vector)的值相加。
- 遍历 HashMap 并打印字谜字符串。
C++
// C++ program for finding all anagram
// pairs in the given array
#include
#include
#include
#include
using namespace std;
// Utility function for
// printing anagram list
void printAnagram(unordered_map >& store)
{
for (auto it:store)
{
vector temp_vec(it.second);
int size = temp_vec.size();
for (int i = 0; i < size; i++)
cout << temp_vec[i] << " ";
cout << "\n";
}
}
// Utility function for storing
// the vector of strings into HashMap
void storeInMap(vector& vec)
{
unordered_map > store;
for (int i = 0; i < vec.size(); i++)
{
string tempString(vec[i]);
// sort the string
sort(tempString.begin(),tempString.end());
// make hash of a sorted string
store[tempString].push_back(vec[i]);
}
// print utility function for printing
// all the anagrams
printAnagram(store);
}
// Driver code
int main()
{
// initialize vector of strings
vector arr;
arr.push_back("geeksquiz");
arr.push_back("geeksforgeeks");
arr.push_back("abcd");
arr.push_back("forgeeksgeeks");
arr.push_back("zuiqkeegs");
arr.push_back("cat");
arr.push_back("act");
arr.push_back("tca");
// utility function for storing
// strings into hashmap
storeInMap(arr);
return 0;
}
Python3
# Python3 program for finding all anagram
# pairs in the given array
from collections import defaultdict
# Utility function for
# printing anagram list
def printAnagram(store: dict) -> None:
for (k, v) in store.items():
temp_vec = v
size = len(temp_vec)
if (size > 1):
for i in range(size):
print(temp_vec[i], end = " ")
print()
# Utility function for storing
# the vector of strings into HashMap
def storeInMap(vec: list) -> None:
store = defaultdict(lambda: list)
for i in range(len(vec)):
tempString = vec[i]
tempString = ''.join(sorted(tempString))
# Check for sorted string
# if it already exists
if (tempString not in store):
temp_vec = []
temp_vec.append(vec[i])
store[tempString] = temp_vec
else:
# Push new string to
# already existing key
temp_vec = store[tempString]
temp_vec.append(vec[i])
store[tempString] = temp_vec
# Print utility function for
# printing all the anagrams
printAnagram(store)
# Driver code
if __name__ == "__main__":
# Initialize vector of strings
arr = []
arr.append("geeksquiz")
arr.append("geeksforgeeks")
arr.append("abcd")
arr.append("forgeeksgeeks")
arr.append("zuiqkeegs")
arr.append("cat")
arr.append("act")
arr.append("tca")
# Utility function for storing
# strings into hashmap
storeInMap(arr)
# This code is contributed by sanjeev2552
注意:在 g++ 中使用 -std=c++11 标志编译上面的程序
输出:
cat act tca
geeksforgeeks forgeeksgeeks
geeksquiz zuiqkeegs
复杂度分析:
- 时间复杂度: O(n * m(log m)),其中 m 是单词的长度。
需要对数组进行一次遍历。 - 空间复杂度: O(n)。
一个字符串有 n 个单词。映射需要 O(n) 空间来存储字符串。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。