📜  给定一个单词序列,一起打印所有字谜 |设置 1

📅  最后修改于: 2021-10-27 08:07:36             🧑  作者: Mango

给定一组单词,一起打印所有字谜。例如,如果给定的数组是 {“cat”, “dog”, “tac”, “god”, “act”},那么输出可能是“cat tac act dog god”。

一个简单的方法是创建一个哈希表。以所有字谜具有相同哈希值的方式计算每个单词的哈希值。用这些哈希值填充哈希表。最后,将这些单词与相同的哈希值一起打印。一个简单的散列机制可以是所有字符的模和。使用模和,两个非字谜词可能具有相同的哈希值。这可以通过匹配单个字符来处理。

以下是将所有字谜一起打印的另一种方法。取两个辅助数组,索引数组和字数组。用给定的单词序列填充单词数组。对单词数组的每个单词进行排序。最后,对单词数组进行排序并跟踪相应的索引。排序后,所有字谜聚集在一起。使用索引阵列从字符串的原始数组打印字符串。

让我们理解以下输入词序列的步骤:

"cat", "dog", "tac", "god", "act"

1)创建两个辅助数组 index[] 和 words[]。将所有给定的单词复制到 words[] 并将原始索引存储在 index[] 中

index[]:  0   1   2   3   4
words[]: cat dog tac god act

2)对words[]中的单个词进行排序。索引数组不会改变。

index[]:   0    1    2    3    4
words[]:  act  dgo  act  dgo  act

3)对words数组进行排序。使用 strcmp() 比较单个单词进行排序

index:     0    2    4    1    3
words[]:  act  act  act  dgo  dgo

4)所有的字谜组合在一起。但是单词在 words 数组中发生了变化。要打印原始单词,请从索引数组中获取索引并在原始数组中使用它。我们得到

"cat tac act dog god"

以下是上述算法的实现。在下面的程序中,使用结构体“Word”的数组来存储索引和单词数组。 DupArray 是另一种存储结构“Word”数组的结构。

C++
// A C++ program to print all anagarms together
#include 
#include 
using namespace std;
 
// structure for each word of duplicate array
class Word {
public:
    char* str; // to store word itself
    int index; // index of the word in the original array
};
 
// structure to represent duplicate array.
class DupArray {
public:
    Word* array; // Array of words
    int size; // Size of array
};
 
// Create a DupArray object that contains an array of Words
DupArray* createDupArray(char* str[], int size)
{
    // Allocate memory for dupArray and all members of it
    DupArray* dupArray = new DupArray();
    dupArray->size = size;
    dupArray->array = new Word[(dupArray->size * sizeof(Word))];
 
    // One by one copy words from the given wordArray to dupArray
    int i;
    for (i = 0; i < size; ++i) {
        dupArray->array[i].index = i;
        dupArray->array[i].str = new char[(strlen(str[i]) + 1)];
        strcpy(dupArray->array[i].str, str[i]);
    }
 
    return dupArray;
}
 
// Compare two characters. Used in qsort() for
// sorting an array of characters (Word)
int compChar(const void* a, const void* b)
{
    return *(char*)a - *(char*)b;
}
 
// Compare two words. Used in qsort()
// for sorting an array of words
int compStr(const void* a, const void* b)
{
    Word* a1 = (Word*)a;
    Word* b1 = (Word*)b;
    return strcmp(a1->str, b1->str);
}
 
// Given a list of words in wordArr[],
void printAnagramsTogether(char* wordArr[], int size)
{
    // Step 1: Create a copy of all words present in given wordArr.
    // The copy will also have original indexes of words
    DupArray* dupArray = createDupArray(wordArr, size);
 
    // Step 2: Iterate through all words in dupArray and sort
    // individual words.
    int i;
    for (i = 0; i < size; ++i)
        qsort(dupArray->array[i].str,
              strlen(dupArray->array[i].str), sizeof(char), compChar);
 
    // Step 3: Now sort the array of words in dupArray
    qsort(dupArray->array, size, sizeof(dupArray->array[0]), compStr);
 
    // Step 4: Now all words in dupArray are together, but these
    // words are changed. Use the index member of word struct to
    // get the corresponding original word
    for (i = 0; i < size; ++i)
        cout << wordArr[dupArray->array[i].index] << " ";
}
 
// Driver program to test above functions
int main()
{
    char* wordArr[] = { "cat", "dog", "tac", "god", "act" };
    int size = sizeof(wordArr) / sizeof(wordArr[0]);
    printAnagramsTogether(wordArr, size);
    return 0;
}
 
// This is code is contributed by rathbhupendra


C
// A C program to print all anagarms together
#include 
#include 
#include 
 
// structure for each word of duplicate array
struct Word {
    char* str; // to store word itself
    int index; // index of the word in the original array
};
 
// structure to represent duplicate array.
struct DupArray {
    struct Word* array; // Array of words
    int size; // Size of array
};
 
// Create a DupArray object that contains an array of Words
struct DupArray* createDupArray(char* str[], int size)
{
    // Allocate memory for dupArray and all members of it
    struct DupArray* dupArray = (struct DupArray*)malloc(sizeof(struct DupArray));
    dupArray->size = size;
    dupArray->array = (struct Word*)malloc(dupArray->size * sizeof(struct Word));
 
    // One by one copy words from the given wordArray to dupArray
    int i;
    for (i = 0; i < size; ++i) {
        dupArray->array[i].index = i;
        dupArray->array[i].str = (char*)malloc(strlen(str[i]) + 1);
        strcpy(dupArray->array[i].str, str[i]);
    }
 
    return dupArray;
}
 
// Compare two characters. Used in qsort() for sorting an array of
// characters (Word)
int compChar(const void* a, const void* b)
{
    return *(char*)a - *(char*)b;
}
 
// Compare two words. Used in qsort() for sorting an array of words
int compStr(const void* a, const void* b)
{
    struct Word* a1 = (struct Word*)a;
    struct Word* b1 = (struct Word*)b;
    return strcmp(a1->str, b1->str);
}
 
// Given a list of words in wordArr[],
void printAnagramsTogether(char* wordArr[], int size)
{
    // Step 1: Create a copy of all words present in given wordArr.
    // The copy will also have original indexes of words
    struct DupArray* dupArray = createDupArray(wordArr, size);
 
    // Step 2: Iterate through all words in dupArray and sort
    // individual words.
    int i;
    for (i = 0; i < size; ++i)
        qsort(dupArray->array[i].str,
              strlen(dupArray->array[i].str), sizeof(char), compChar);
 
    // Step 3: Now sort the array of words in dupArray
    qsort(dupArray->array, size, sizeof(dupArray->array[0]), compStr);
 
    // Step 4: Now all words in dupArray are together, but these
    // words are changed. Use the index member of word struct to
    // get the corresponding original word
    for (i = 0; i < size; ++i)
        printf("%s ", wordArr[dupArray->array[i].index]);
}
 
// Driver program to test above functions
int main()
{
    char* wordArr[] = { "cat", "dog", "tac", "god", "act" };
    int size = sizeof(wordArr) / sizeof(wordArr[0]);
    printAnagramsTogether(wordArr, size);
    return 0;
}


Java
// A Java program to print all anagrams together
import java.util.Arrays;
import java.util.Comparator;
public class GFG {
    // class for each word of duplicate array
    static class Word {
        String str; // to store word itself
        int index; // index of the word in the
        // original array
 
        // constructor
        Word(String str, int index)
        {
            this.str = str;
            this.index = index;
        }
    }
 
    // class to represent duplicate array.
    static class DupArray {
        Word[] array; // Array of words
        int size; // Size of array
 
        // constructor
        public DupArray(String str[], int size)
        {
            this.size = size;
            array = new Word[size];
 
            // One by one copy words from the
            // given wordArray to dupArray
            int i;
            for (i = 0; i < size; ++i) {
                // create a word Object with the
                // str[i] as str and index as i
                array[i] = new Word(str[i], i);
            }
        }
    }
 
    // Compare two words. Used in Arrays.sort() for
    // sorting an array of words
    static class compStr implements Comparator {
        public int compare(Word a, Word b)
        {
            return a.str.compareTo(b.str);
        }
    }
 
    // Given a list of words in wordArr[],
    static void printAnagramsTogether(String wordArr[],
                                      int size)
    {
        // Step 1: Create a copy of all words present
        // in given wordArr. The copy will also have
        // original indexes of words
        DupArray dupArray = new DupArray(wordArr, size);
 
        // Step 2: Iterate through all words in
        // dupArray and sort individual words.
        int i;
        for (i = 0; i < size; ++i) {
            char[] char_arr = dupArray.array[i].str.toCharArray();
            Arrays.sort(char_arr);
            dupArray.array[i].str = new String(char_arr);
        }
 
        // Step 3: Now sort the array of words in
        // dupArray
        Arrays.sort(dupArray.array, new compStr());
 
        // Step 4: Now all words in dupArray are together,
        // but these words are changed. Use the index
        // member of word struct to get the corresponding
        // original word
        for (i = 0; i < size; ++i)
            System.out.print(wordArr[dupArray.array[i].index] + " ");
    }
 
    // Driver program to test above functions
    public static void main(String args[])
    {
        String wordArr[] = { "cat", "dog", "tac", "god", "act" };
        int size = wordArr.length;
        printAnagramsTogether(wordArr, size);
    }
}
// This code is contributed by Sumit Ghosh


Python
# A Python program to print all anagarms together
 
# structure for each word of duplicate array
class Word(object):
    def __init__(self, string, index):
        self.string = string
        self.index = index
 
# Create a DupArray object that contains an array
# of Words
def createDupArray(string, size):
    dupArray = []
 
    # One by one copy words from the given wordArray
    # to dupArray
    for i in xrange(size):
        dupArray.append(Word(string[i], i))
 
    return dupArray
 
# Given a list of words in wordArr[]
def printAnagramsTogether(wordArr, size):
    # Step 1: Create a copy of all words present in
    # given wordArr.
    # The copy will also have original indexes of words
    dupArray = createDupArray(wordArr, size)
 
    # Step 2: Iterate through all words in dupArray and sort
    # individual words.
    for i in xrange(size):
        dupArray[i].string = ''.join(sorted(dupArray[i].string))
 
    # Step 3: Now sort the array of words in dupArray
    dupArray = sorted(dupArray, key = lambda k: k.string)
 
    # Step 4: Now all words in dupArray are together, but
    # these words are changed. Use the index member of word
    # struct to get the corresponding original word
    for word in dupArray:
        print wordArr[word.index],
 
# Driver program
wordArr = ["cat", "dog", "tac", "god", "act"]
size = len(wordArr)
printAnagramsTogether(wordArr, size)
 
# This code is contributed by BHAVYA JAIN


C++
// C++ program to print anagrams
// together using dictionary
#include
using namespace std;
 
void printAnagrams(string arr[], int size)
{
    unordered_map> map;
 
    // Loop over all words
    for(int i = 0; i < size; i++)
    {
         
        // Convert to char array, sort and
        // then re-convert to string
        string word = arr[i];
        char letters[word.size() + 1];
        strcpy(letters, word.c_str());
        sort(letters, letters + word.size() + 1);
        string newWord = "";
        for(int i = 0; i < word.size() + 1; i++)
        {
            newWord += letters[i];
        }
         
        // Calculate hashcode of string
        // after sorting
        if (map.find(newWord) != map.end())
        {
            map[newWord].push_back(word);
        }
        else
        {
             
            // This is the first time we are
            // adding a word for a specific
            // hashcode
            vector words;
            words.push_back(word);
            map[newWord] = words;
        }
    }
     
    // Print all the values where size is > 1
    // If you want to print non-anagrams,
    // just print the values having size = 1
    unordered_map>::iterator it;
    for(it = map.begin(); it != map.end(); it++)
    {
        vector values = map[it->first];
        if (values.size() > 1)
        {
            cout << "[";
            for(int i = 0; i < values.size() - 1; i++)
            {
                cout << values[i] << ", ";
            }
            cout << values[values.size() - 1];
            cout << "]";
        }
    }
}
 
// Driver code
int main()
{
    string arr[] = { "cat", "dog", "tac",
                     "god", "act" };
    int size = sizeof(arr) / sizeof(arr[0]);
     
    printAnagrams(arr, size);
     
    return 0;
}
 
// This code is contributed by Ankit Garg


Java
// Java program to print anagrams
// together using dictionary
import java.util.*;
 
public class FindAnagrams {
 
    private static void printAnagrams(String arr[])
    {
        HashMap > map = new HashMap<>();
 
        // loop over all words
        for (int i = 0; i < arr.length; i++) {
 
            // convert to char array, sort and
            // then re-convert to string
            String word = arr[i];
            char[] letters = word.toCharArray();
            Arrays.sort(letters);
            String newWord = new String(letters);
 
            // calculate hashcode of string
            // after sorting
            if (map.containsKey(newWord)) {
 
                map.get(newWord).add(word);
            }
            else {
 
                // This is the first time we are
                // adding a word for a specific
                // hashcode
                List words = new ArrayList<>();
                words.add(word);
                map.put(newWord, words);
            }
        }
 
        // print all the values where size is > 1
        // If you want to print non-anagrams,
        // just print the values having size = 1
        for (String s : map.keySet()) {
            List values = map.get(s);
            if (values.size() > 1) {
                System.out.print(values);
            }
        }
    }
 
    public static void main(String[] args)
    {
 
        // Driver program
        String arr[] = { "cat", "dog", "tac", "god", "act" };
        printAnagrams(arr);
    }
}


Python3
from collections import defaultdict
 
def printAnagramsTogether(words):
    groupedWords = defaultdict(list)
 
    # Put all anagram words together in a dictionary
    # where key is sorted word
    for word in words:
        groupedWords["".join(sorted(word))].append(word)
 
    # Print all anagrams together
    for group in groupedWords.values():
        print(" ".join(group))     
 
 
if __name__ == "__main__":  
    arr =  ["cat", "dog", "tac", "god", "act"] 
    printAnagramsTogether(arr)


C#
// C# program to print anagrams
// together using dictionary
using System;
using System.Collections.Generic;
 
class GFG {
    private static void printAnagrams(String[] arr)
    {
        Dictionary >
            map = new Dictionary >();
 
        // loop over all words
        for (int i = 0; i < arr.Length; i++) {
 
            // convert to char array, sort and
            // then re-convert to string
            String word = arr[i];
            char[] letters = word.ToCharArray();
            Array.Sort(letters);
            String newWord = new String(letters);
 
            // calculate hashcode of string
            // after sorting
            if (map.ContainsKey(newWord)) {
                map[newWord].Add(word);
            }
            else {
 
                // This is the first time we are
                // adding a word for a specific
                // hashcode
                List words = new List();
                words.Add(word);
                map.Add(newWord, words);
            }
        }
 
        // print all the values where size is > 1
        // If you want to print non-anagrams,
        // just print the values having size = 1
        List value = new List();
        foreach(KeyValuePair >
                    entry in map)
        {
            value.Add(entry.Key);
        }
        int k = 0;
        foreach(KeyValuePair >
                    entry in map)
        {
            List values = map[value[k++]];
            if (values.Count > 1) {
                Console.Write("[");
                int len = 1;
                foreach(String s in values)
                {
                    Console.Write(s);
                    if (len++ < values.Count)
                        Console.Write(", ");
                }
                Console.Write("]");
            }
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String[] arr = { "cat", "dog", "tac",
                         "god", "act" };
        printAnagrams(arr);
    }
}
 
// This code is contributed by Princi Singh


C++
// C++ code to print all anagrams together
#include 
using namespace std;
 
void solver(vector my_list)
{
     
    // Inner hashmap counts frequency
    // of characters in a string.
    // Outer hashmap for if same
    // frequency characters are present in
    // in a string then it will add it to
    // the vector.
    map, vector> my_map;
     
    // Loop over all words
    for(string str : my_list)
    {
         
        // Counting the frequency of the
        // characters present in a string
        map temp_map;
        vector temp_my_list;
        for(int i = 0; i < str.length(); ++i)
        {
            ++temp_map[str[i]];
        }
         
        // If the same frequency of chanracters
        // are alraedy present then add that
        // string into that arraylist otherwise
        // created a new arraylist and add that
        // string
        auto it = my_map.find(temp_map);
        if (it != my_map.end())
        {
            it->second.push_back(str);
        }
        else
        {
            temp_my_list.push_back(str);
            my_map.insert({ temp_map, temp_my_list });
        }
    }
     
    // Stores the result in a vector
    vector> result;
 
    for(auto it = my_map.begin();
             it != my_map.end(); ++it)
    {
        result.push_back(it->second);
    }
 
    for(int i = 0; i < result.size(); ++i)
    {
          cout << "[";
        for(int j = 0; j < result[i].size(); ++j)
        {
            cout << result[i][j] << ", ";
        }
          cout << "]";
    }
}
 
// Driver code
int main()
{
    vector my_list = { "cat", "dog", "ogd",
                               "god", "atc" };
    solver(my_list);
    return 0;
}
 
// This code is contributed by
// Apurba Kumar Gorai(coolapurba05)


Java
// Java code tp print all anagrams together
import java.util.ArrayList;
import java.util.HashMap;
 
public class FindAnagrams {
 
    private static ArrayList >
    solver(
        ArrayList list)
    {
 
        // Inner hashmap counts frequency
        // of characters in a string.
        // Outer hashmap for if same
        // frequency characters are present in
        // in a string then it will add it to
        // the arraylist.
        HashMap,
                ArrayList >
            map = new HashMap,
                              ArrayList >();
        for (String str : list) {
            HashMap
                tempMap = new HashMap();
 
            // Counting the frequency of the
            // characters present in a string
            for (int i = 0; i < str.length(); i++) {
                if (tempMap.containsKey(str.charAt(i))) {
                    int x = tempMap.get(str.charAt(i));
                    tempMap.put(str.charAt(i), ++x);
                }
                else {
                    tempMap.put(str.charAt(i), 1);
                }
            }
 
            // If the same frequency of chanracters
            // are alraedy present then add that
            // string into that arraylist otherwise
            // created a new arraylist and add that string
            if (map.containsKey(tempMap))
                map.get(tempMap).add(str);
            else {
                ArrayList
                    tempList = new ArrayList();
                tempList.add(str);
                map.put(tempMap, tempList);
            }
        }
 
        // Stores the result in a arraylist
        ArrayList >
            result = new ArrayList<>();
        for (HashMap
                 temp : map.keySet())
            result.add(map.get(temp));
        return result;
    }
 
    // Drivers Method
    public static void main(String[] args)
    {
        ArrayList list = new ArrayList<>();
        list.add("cat");
        list.add("dog");
        list.add("ogd");
        list.add("god");
        list.add("atc");
 
        System.out.println(solver(list));
    }
}
 
// This code is contributed by Arijit Basu(ArijitXfx)


Python3
# Python code to print all anagrams together
from collections import Counter, defaultdict
user_input = ["cat", "dog", "tac", "edoc", "god", "tacact",
              "act", "code", "deno", "node", "ocde", "done", "catcat"]
 
 
def solve(words: list) -> list:
    # defaultdict will create a new list if the key is not found in the dictionary
    m = defaultdict(list)
 
    # loop over all the words
    for word in words:
        # Counter('cat') :
        #    counts the frequency of the characters present in a string
        #    >>> Counter({'c': 1, 'a': 1, 't': 1})
 
        # frozenset(dict(Counter('cat')).items()) :
        #    frozenset takes an iterable object as input and makes them immutable.
        #    So that hash(frozenset(Counter('cat'))) is equal to
        #   hash of other 'cat' anagrams
        #    >>> frozenset({('c', 1), ('a', 1), ('t', 1)})
        m[frozenset(dict(Counter(word)).items())].append(word)
    return [v for k, v in m.items()]
 
 
print(solve(user_input))
 
# This code is contributed by
# Rohan Kumar(@r0hnx)


输出:

cat tac act dog god 

时间复杂度:假设有 N 个单词,每个单词最多有 M 个字符。上限为 O(NMLogM + MNLogN)。
第 2 步需要 O(NMLogM) 时间。对单词进行排序最多需要 O(MLogM) 时间。所以排序 N 个单词需要 O(NMLogM) 时间。步骤 3 需要 O(MNLogN) 排序单词数组需要 NLogN 比较。比较可能需要最大 O(M) 时间。所以对单词数组进行排序的时间将是 O(MNLogN)。

使用哈希图
在这里,我们首先对每个单词进行排序,以排序后的单词作为关键字,然后将原始单词放入映射中。地图的值将是一个列表,其中包含排序后具有相同单词的所有单词。
最后,我们将打印哈希图中的所有值,其中值的大小将大于 1。

C++

// C++ program to print anagrams
// together using dictionary
#include
using namespace std;
 
void printAnagrams(string arr[], int size)
{
    unordered_map> map;
 
    // Loop over all words
    for(int i = 0; i < size; i++)
    {
         
        // Convert to char array, sort and
        // then re-convert to string
        string word = arr[i];
        char letters[word.size() + 1];
        strcpy(letters, word.c_str());
        sort(letters, letters + word.size() + 1);
        string newWord = "";
        for(int i = 0; i < word.size() + 1; i++)
        {
            newWord += letters[i];
        }
         
        // Calculate hashcode of string
        // after sorting
        if (map.find(newWord) != map.end())
        {
            map[newWord].push_back(word);
        }
        else
        {
             
            // This is the first time we are
            // adding a word for a specific
            // hashcode
            vector words;
            words.push_back(word);
            map[newWord] = words;
        }
    }
     
    // Print all the values where size is > 1
    // If you want to print non-anagrams,
    // just print the values having size = 1
    unordered_map>::iterator it;
    for(it = map.begin(); it != map.end(); it++)
    {
        vector values = map[it->first];
        if (values.size() > 1)
        {
            cout << "[";
            for(int i = 0; i < values.size() - 1; i++)
            {
                cout << values[i] << ", ";
            }
            cout << values[values.size() - 1];
            cout << "]";
        }
    }
}
 
// Driver code
int main()
{
    string arr[] = { "cat", "dog", "tac",
                     "god", "act" };
    int size = sizeof(arr) / sizeof(arr[0]);
     
    printAnagrams(arr, size);
     
    return 0;
}
 
// This code is contributed by Ankit Garg

Java

// Java program to print anagrams
// together using dictionary
import java.util.*;
 
public class FindAnagrams {
 
    private static void printAnagrams(String arr[])
    {
        HashMap > map = new HashMap<>();
 
        // loop over all words
        for (int i = 0; i < arr.length; i++) {
 
            // convert to char array, sort and
            // then re-convert to string
            String word = arr[i];
            char[] letters = word.toCharArray();
            Arrays.sort(letters);
            String newWord = new String(letters);
 
            // calculate hashcode of string
            // after sorting
            if (map.containsKey(newWord)) {
 
                map.get(newWord).add(word);
            }
            else {
 
                // This is the first time we are
                // adding a word for a specific
                // hashcode
                List words = new ArrayList<>();
                words.add(word);
                map.put(newWord, words);
            }
        }
 
        // print all the values where size is > 1
        // If you want to print non-anagrams,
        // just print the values having size = 1
        for (String s : map.keySet()) {
            List values = map.get(s);
            if (values.size() > 1) {
                System.out.print(values);
            }
        }
    }
 
    public static void main(String[] args)
    {
 
        // Driver program
        String arr[] = { "cat", "dog", "tac", "god", "act" };
        printAnagrams(arr);
    }
}

蟒蛇3

from collections import defaultdict
 
def printAnagramsTogether(words):
    groupedWords = defaultdict(list)
 
    # Put all anagram words together in a dictionary
    # where key is sorted word
    for word in words:
        groupedWords["".join(sorted(word))].append(word)
 
    # Print all anagrams together
    for group in groupedWords.values():
        print(" ".join(group))     
 
 
if __name__ == "__main__":  
    arr =  ["cat", "dog", "tac", "god", "act"] 
    printAnagramsTogether(arr)    

C#

// C# program to print anagrams
// together using dictionary
using System;
using System.Collections.Generic;
 
class GFG {
    private static void printAnagrams(String[] arr)
    {
        Dictionary >
            map = new Dictionary >();
 
        // loop over all words
        for (int i = 0; i < arr.Length; i++) {
 
            // convert to char array, sort and
            // then re-convert to string
            String word = arr[i];
            char[] letters = word.ToCharArray();
            Array.Sort(letters);
            String newWord = new String(letters);
 
            // calculate hashcode of string
            // after sorting
            if (map.ContainsKey(newWord)) {
                map[newWord].Add(word);
            }
            else {
 
                // This is the first time we are
                // adding a word for a specific
                // hashcode
                List words = new List();
                words.Add(word);
                map.Add(newWord, words);
            }
        }
 
        // print all the values where size is > 1
        // If you want to print non-anagrams,
        // just print the values having size = 1
        List value = new List();
        foreach(KeyValuePair >
                    entry in map)
        {
            value.Add(entry.Key);
        }
        int k = 0;
        foreach(KeyValuePair >
                    entry in map)
        {
            List values = map[value[k++]];
            if (values.Count > 1) {
                Console.Write("[");
                int len = 1;
                foreach(String s in values)
                {
                    Console.Write(s);
                    if (len++ < values.Count)
                        Console.Write(", ");
                }
                Console.Write("]");
            }
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String[] arr = { "cat", "dog", "tac",
                         "god", "act" };
        printAnagrams(arr);
    }
}
 
// This code is contributed by Princi Singh

输出 :

[cat, tac, act][dog, god]

具有 O(NM) 解决方案的 HashMap
在之前的方法中,我们对每个字符串进行排序以维护一个相似的键,但是这种方法中的额外时间将利用另一个哈希图来维护字符的频率,这将为不同的字符串生成相同的哈希函数具有相同频率的字符。
在这里,我们将采用 HashMap,内部 HashMap 将计算每个字符串的字符出现的频率,外部 HashMap 将检查该哈希图是否存在(如果存在),然后将该字符串添加到相应的列表中.

C++

// C++ code to print all anagrams together
#include 
using namespace std;
 
void solver(vector my_list)
{
     
    // Inner hashmap counts frequency
    // of characters in a string.
    // Outer hashmap for if same
    // frequency characters are present in
    // in a string then it will add it to
    // the vector.
    map, vector> my_map;
     
    // Loop over all words
    for(string str : my_list)
    {
         
        // Counting the frequency of the
        // characters present in a string
        map temp_map;
        vector temp_my_list;
        for(int i = 0; i < str.length(); ++i)
        {
            ++temp_map[str[i]];
        }
         
        // If the same frequency of chanracters
        // are alraedy present then add that
        // string into that arraylist otherwise
        // created a new arraylist and add that
        // string
        auto it = my_map.find(temp_map);
        if (it != my_map.end())
        {
            it->second.push_back(str);
        }
        else
        {
            temp_my_list.push_back(str);
            my_map.insert({ temp_map, temp_my_list });
        }
    }
     
    // Stores the result in a vector
    vector> result;
 
    for(auto it = my_map.begin();
             it != my_map.end(); ++it)
    {
        result.push_back(it->second);
    }
 
    for(int i = 0; i < result.size(); ++i)
    {
          cout << "[";
        for(int j = 0; j < result[i].size(); ++j)
        {
            cout << result[i][j] << ", ";
        }
          cout << "]";
    }
}
 
// Driver code
int main()
{
    vector my_list = { "cat", "dog", "ogd",
                               "god", "atc" };
    solver(my_list);
    return 0;
}
 
// This code is contributed by
// Apurba Kumar Gorai(coolapurba05)

Java

// Java code tp print all anagrams together
import java.util.ArrayList;
import java.util.HashMap;
 
public class FindAnagrams {
 
    private static ArrayList >
    solver(
        ArrayList list)
    {
 
        // Inner hashmap counts frequency
        // of characters in a string.
        // Outer hashmap for if same
        // frequency characters are present in
        // in a string then it will add it to
        // the arraylist.
        HashMap,
                ArrayList >
            map = new HashMap,
                              ArrayList >();
        for (String str : list) {
            HashMap
                tempMap = new HashMap();
 
            // Counting the frequency of the
            // characters present in a string
            for (int i = 0; i < str.length(); i++) {
                if (tempMap.containsKey(str.charAt(i))) {
                    int x = tempMap.get(str.charAt(i));
                    tempMap.put(str.charAt(i), ++x);
                }
                else {
                    tempMap.put(str.charAt(i), 1);
                }
            }
 
            // If the same frequency of chanracters
            // are alraedy present then add that
            // string into that arraylist otherwise
            // created a new arraylist and add that string
            if (map.containsKey(tempMap))
                map.get(tempMap).add(str);
            else {
                ArrayList
                    tempList = new ArrayList();
                tempList.add(str);
                map.put(tempMap, tempList);
            }
        }
 
        // Stores the result in a arraylist
        ArrayList >
            result = new ArrayList<>();
        for (HashMap
                 temp : map.keySet())
            result.add(map.get(temp));
        return result;
    }
 
    // Drivers Method
    public static void main(String[] args)
    {
        ArrayList list = new ArrayList<>();
        list.add("cat");
        list.add("dog");
        list.add("ogd");
        list.add("god");
        list.add("atc");
 
        System.out.println(solver(list));
    }
}
 
// This code is contributed by Arijit Basu(ArijitXfx)

蟒蛇3

# Python code to print all anagrams together
from collections import Counter, defaultdict
user_input = ["cat", "dog", "tac", "edoc", "god", "tacact",
              "act", "code", "deno", "node", "ocde", "done", "catcat"]
 
 
def solve(words: list) -> list:
    # defaultdict will create a new list if the key is not found in the dictionary
    m = defaultdict(list)
 
    # loop over all the words
    for word in words:
        # Counter('cat') :
        #    counts the frequency of the characters present in a string
        #    >>> Counter({'c': 1, 'a': 1, 't': 1})
 
        # frozenset(dict(Counter('cat')).items()) :
        #    frozenset takes an iterable object as input and makes them immutable.
        #    So that hash(frozenset(Counter('cat'))) is equal to
        #   hash of other 'cat' anagrams
        #    >>> frozenset({('c', 1), ('a', 1), ('t', 1)})
        m[frozenset(dict(Counter(word)).items())].append(word)
    return [v for k, v in m.items()]
 
 
print(solve(user_input))
 
# This code is contributed by
# Rohan Kumar(@r0hnx)

输出:

[[cat, atc], [dog, ogd, god]]

时间复杂度:假设有 N 个单词,每个单词最多有 M 个字符。上限是 O(NM)。
空间复杂度:假设有 N 个单词,每个单词最多有 M 个字符。上限是 O(N+M)。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程