C++ program to group anagrams in the given stream of strings28 Aug 2024 | 5 min read An anagram is a word formed by rearranging the letters of another word, such as "listen" and "silent". To group anagrams in each stream of strings, we need to group all the strings that are anagrams of each other together. Example 1:A code snippet in C++ that uses a hash table to group anagrams: Output bat tan nat eat tea ate In this implementation, we first create an unordered map hashTable where the keys are sorted strings and the values are vectors of strings that are anagrams of each other. After that, we iterate through each string in the input vector strs, sort the string to form a key and add it to the hash table. Finally, we clear the input vector "strs" and iterate through the hash table to append the anagram groups to strs. There are other approaches to grouping anagrams in a stream of strings in C++. One such approach is to use a vector of pairs to store the sorted string as the first element of the pair and the original string as the second element of the pair. After that, we can sort this vector of pairs based on the sorted string, and the anagrams will be adjacent to each other. Finally, we can extract the original strings and store them in a separate vector. Example 2:Here is an implementation of this approach: Output bat ate eat tea nat tan In this implementation, we first create a vector of pairs sortedStrs, where the first element of each pair is the sorted string and the second element is the original string. After that, we iterate through each string in the input vector strs, sort the string to form the sorted string, and add the pair to sortedStrs. We sort sortedStrs based on the sorted strings using std::sort(), which orders the pairs lexicographically based on their first elements. Finally, we clear the input vector "strs" and iterate through sortedStrs to extract the second element of each pair and append it to "strs". This approach has a time complexity of O(N * M * log M), where N is the number of strings in the input vector and M is the length of the longest string. The space complexity is O(N * M), as we need to store the sorted strings for each input string. Example 3:Another way to implement this is using the counting sort approach: Output bat tan nat eat tea ate In this implementation, we first create an unordered map map, where the key is the sorted string based on the count of each character in the string, and the value is a vector of the original strings that have the same sorted string. After that, we iterate through each string in the input vector strs, count the frequency of each character using a counting array count, and construct the key by concatenating the count of each character with a "#" separator. We insert the original string into the value vector corresponding to the key in map. Finally, we clear the input vector strs and iterate through map to extract the value vectors and append their elements to strs. There are several other approaches such as:
All these approaches have their own advantages and disadvantages, and the optimal approach depends on the size of the input vector, the length of the strings, and the available memory. Next Topiccstdlib in C++ |
C++ Program to print Number Triangle
Like alphabet triangle, we can write the C++ program to print the number triangle. The number triangle can be printed in different ways. Let's see the C++ example to print number triangle. Example #include <iostream> using namespace std; int main() { int i,j,k,l,n; cout<<"Enter the Range="; cin>>n; for(i=1;i<=n;i++) ...
1 min read
Encapsulation in C++
In C++, OOP encapsulation refers to the grouping of data and related functions inside a single class. In other words, encapsulation is defined as the Binding (or wrapping) code and data together into a single unit. It restricts direct access to data and allows controlled modification...
9 min read
std::Chrono::Time_point in C++
In this article, we will discuss the std::chrono::time_point in C++ with its example. A class template called std::chrono::time_point is included in the <chrono> header of the C++ Standard Library. It is used to handle computations involving time and represents a particular point in time. Template Specifications: Clock: This time point...
2 min read
C++ Program for Counting Inversions in an Array
In this article, we will discuss C++ program for counting inversions in an array with several methods. What is Inversion Count? Inversion Count for an array indicates - how far (or close) the array is from being sorted. If the array is already sorted, the inversion count is...
6 min read
is_trivial in C++
Over time, C++'s weaponry of features and type traits expands as the language continually improves, giving developers a stronghold to enforce correctness and effectiveness in their codes. The is_trivial is one of the traits of one type, which has a significant role in metaprogramming and template-based...
3 min read
C++ Program to Draw Histogram
Introduction to Histograms and Their Use Cases The frequency distribution of a collection of data is graphically represented using histograms. They are frequently used to visualize and analyze data in scientific research, statistics, and data analysis. A histogram comprises a sequence of vertical bars, where each bar's...
9 min read
Std::allocator() in C++
Efficient memory management is paramount in the world of C++, where building resilient and high-performance applications hinges on optimal resource utilization. At the core of this endeavor lies the std::allocator class, a foundational element for dynamic memory allocation. In this article, we embark on a journey...
4 min read
Find the original number by flipping K unique bits in C++
Introduction: As finding the original number by flipping K unique bits is an interesting problem in C++ which concerns the interpretation of digital encoding of a number transforming certain bits of it. In the digital world, each integer is expressed in binary, which is a binary digit...
5 min read
Isprint() in C++
isprint() is a predefined function in C++ that handles strings and characters. The header files needed for string and character functions are cstring and cctype, respectively. If the argument has any printable characters, this function is utilised to determine that fact. In C++, there are numerous...
4 min read
Type Inference in C++ (auto and decltype)
In a programming language like C or C++, we declare any variable and explicitly declare the data type of the variable at the compile time. But type inference means we use some keywords by which we need not to declare the data type of the variable...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India