Question

In: Computer Science

Write a program (O(n), where n is the number of words) that takes as input a...

Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here Do not change anything in the test file.

CPP File:

#include
#include
#include
#include

using namespace std;
vector> findAnagrams(const vector& dict);

vector> findAnagrams(const vector& dict)
{

// Your code here...


}

Test File:

#include
#include
#include
#include

using namespace std;
vector> findAnagrams(const vector& dict);

int main()
{
vector word_list = {"debitcard", "elvis", "silent", "badcredit", "lives", "freedom", "listen", "levis"};
vector> result = findAnagrams(word_list);
for (auto anagrams: result) {
for (auto words: anagrams)
cout << words << " ";
cout << endl;
}
return 0;

/* Output should be -

silent listen
debitcard badcredit
elvis lives levis

*/
}

The expected output should be in this order ONLY:

freedom

silent listen

debitcard badcredit

elvis lives levis

Solutions

Expert Solution

code snippet for the function findAnagrams:

{

// Your code here...
        vector<string>original, new_list;
        new_list.insert(new_list.end(), dict.begin(), dict.end());

        for (string &s : new_list)
                sort(s.begin(), s.end());

        unordered_map<string, vector<int>> map;
        for (int i = 0; i < dict.size(); i++)
                map[new_list[i]].push_back(i);

        vector<vector<string>> ans;

        for (auto itr : map)
        {
                vector<string>tmp;
                for (int index : itr.second)
                        tmp.push_back(dict[index]);

                ans.push_back(tmp);
        }

        return ans;

}

Complete code including driver main function to run in single file:

#include<bits/stdc++.h>
using namespace std;
vector<vector<string>> findAnagrams(const vector<string>&dict)
{

// Your code here...
        vector<string>original, new_list;
        new_list.insert(new_list.end(), dict.begin(), dict.end());

        for (string &s : new_list)
                sort(s.begin(), s.end());

        unordered_map<string, vector<int>> map;
        for (int i = 0; i < dict.size(); i++)
                map[new_list[i]].push_back(i);

        vector<vector<string>> ans;

        for (auto itr : map)
        {
                vector<string>tmp;
                for (int index : itr.second)
                        tmp.push_back(dict[index]);

                ans.push_back(tmp);
        }

        return ans;

}

int main()
{
        vector<string>word_list= {"debitcard", "elvis", "silent", "badcredit", "lives", "freedom", "listen", "levis"};
        vector<vector<string>> result = findAnagrams(word_list);
        for (auto anagrams : result) {
                for (auto words : anagrams)
                        cout << words << " ";
                cout << endl;
                
        }
        return 0;
}

EXPLANATION:

created another list and sorted each word to create a index for individual anagramic strings and after that created a map to store the index of all anagramic strings together (grouped). After that interated through the map and stored the same grouped anagramic strings together and returned the vector of vector strings.

NOTE: if any doubt or problem with the code let me know in the comment section and do upvote.


Related Solutions

Write a function in Matlab that takes as input the number n and a symmetric tridiagonal...
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal matrix given as two vectors: n×1 vector v representing the main diagonal and (n−1)×1 vector w representing the upper diagonal. Have this function output the Cholesky factor of the matrix as a vector for the main diagonal and a vector for the upper diagonal and output the number of flops and, separately, the number of square roots used as well. Use only basic programming....
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
Write a program in C that takes as input an 8-bit binary number and prints the...
Write a program in C that takes as input an 8-bit binary number and prints the next 10 binary numbers. Define a binary number as int binNum[8]; Use binNum[0] to store the most significant (i.e., leftmost) bit and binNum[7] to store the least significant bit. Ask the user to input the first binary number with each bit separated by at least one space.
Write a program in C that takes as input a four-digit hexadecimal number and prints the...
Write a program in C that takes as input a four-digit hexadecimal number and prints the next 10 hexadecimal numbers. Define a hexadecimal number as int hexNum[4] Allow upper- or lowercase letters for input and use uppercase letters for the hexadecimal output. For example, 3C6f should be valid input and should produce output 3C6F, 3C70, 3C71, . . . .
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
Must be written in JAVA Code Write a program that takes a whole number input from...
Must be written in JAVA Code Write a program that takes a whole number input from a user and determines whether it’s prime. If the number is not prime, display its unique prime factors. Remember that a prime number’s factors are only 1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied...
Write a Little Man program that outputs the sum of n number of input values.
Write a Little Man program that outputs the sum of n number of input values.
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT)...
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT) AND CALCULATES THE NTH FIBONACCI SUM USING RECURSION. EXAMPLE: OLD VERSION 0,1,1,2,3.. USING USER INPUT: 0, N, M ,N+M, (N+M)+M PART 2: WRITE THE SAME PROGRAM USING USER INPUT BUT USING A LOOP IN STEAD OF RECURSION. PYTHON
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT