Given a list of words with lower cases. Implement a function to find all Words that have the same unique character set.
Example:
Input: words[] = { "may", "student", "students", "dog",
"studentssess", "god", "cat", "act",
"tab", "bat", "flow", "wolf", "lambs",
"amy", "yam", "balms", "looped",
"poodle"};
Output :
looped, poodle,
lambs, balms,
flow, wolf,
tab, bat,
may, amy, yam,
student, students, studentssess,
dog, god,
cat, act,
All words with same set of characters are printed
together in a line.
The idea is to use hashing. We generate a key for all words. The key contains all unique characters (The size of the key is at most 26 for lowercase alphabets). We store indexes of words as values for a key. Once we have filled all keys and values in the hash table, we can print the result by traversing the table.
Below is the implementation of the above idea.
C++
#include<bits/stdc++.h>
using namespace std;
#define MAX_CHAR 26
string getKey(string &str)
{
bool visited[MAX_CHAR] = { false };
for ( int j = 0; j < str.length(); j++)
visited[str[j] - 'a' ] = true ;
string key = "" ;
for ( int j=0; j < MAX_CHAR; j++)
if (visited[j])
key = key + ( char )( 'a' +j);
return key;
}
void wordsWithSameCharSet(string words[], int n)
{
unordered_map <string, vector < int > > Hash;
for ( int i=0; i<n; i++)
{
string key = getKey(words[i]);
Hash[key].push_back(i);
}
for ( auto it = Hash.begin(); it!=Hash.end(); it++)
{
for ( auto v=(*it).second.begin(); v!=(*it).second.end(); v++)
cout << words[*v] << ", " ;
cout << endl;
}
}
int main()
{
string words[] = { "may" , "student" , "students" , "dog" ,
"studentssess" , "god" , "cat" , "act" , "tab" ,
"bat" , "flow" , "wolf" , "lambs" , "amy" , "yam" ,
"balms" , "looped" , "poodle" };
int n = sizeof (words)/ sizeof (words[0]);
wordsWithSameCharSet(words, n);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
public class GFG {
static final int MAX_CHAR = 26 ;
static String getKey(String str)
{
boolean [] visited = new boolean [MAX_CHAR];
Arrays.fill(visited, false );
for ( int j = 0 ; j < str.length(); j++)
visited[str.charAt(j) - 'a' ] = true ;
String key = "" ;
for ( int j= 0 ; j < MAX_CHAR; j++)
if (visited[j])
key = key + ( char )( 'a' +j);
return key;
}
static void wordsWithSameCharSet(String words[], int n)
{
HashMap<String, ArrayList<Integer>> Hash = new HashMap<>();
for ( int i= 0 ; i<n; i++)
{
String key = getKey(words[i]);
if (Hash.containsKey(key))
{
ArrayList<Integer> get_al = Hash.get(key);
get_al.add(i);
Hash.put(key, get_al);
}
else
{
ArrayList<Integer> new_al = new ArrayList<>();
new_al.add(i);
Hash.put(key, new_al);
}
}
for (Entry<String, ArrayList<Integer>> it : Hash.entrySet())
{
ArrayList<Integer> get =it.getValue();
for (Integer v:get)
System.out.print( words[v] + ", " );
System.out.println();
}
}
public static void main(String args[])
{
String words[] = { "may" , "student" , "students" , "dog" ,
"studentssess" , "god" , "cat" , "act" , "tab" ,
"bat" , "flow" , "wolf" , "lambs" , "amy" , "yam" ,
"balms" , "looped" , "poodle" };
int n = words.length;
wordsWithSameCharSet(words, n);
}
}
|
Python3
from collections import Counter
def groupStrings( input ):
dict = {}
for word in input :
wordDict = Counter(word)
key = wordDict.keys()
key = sorted (key)
key = ''.join(key)
if key in dict .keys():
dict [key].append(word)
else :
dict [key] = []
dict [key].append(word)
for (key,value) in dict .items():
print ( ',' .join( dict [key]))
if __name__ = = "__main__" :
input = [ 'may' , 'student' , 'students' , 'dog' , 'studentssess' , 'god' , 'cat' , 'act' , 'tab' , 'bat' , 'flow' , 'wolf' , 'lambs' , 'amy' , 'yam' , 'balms' , 'looped' , 'poodle' ]
groupStrings( input )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static readonly int MAX_CHAR = 26;
static String getKey(String str)
{
bool [] visited = new bool [MAX_CHAR];
for ( int j = 0; j < str.Length; j++)
visited[str[j] - 'a' ] = true ;
String key = "" ;
for ( int j = 0; j < MAX_CHAR; j++)
if (visited[j])
key = key + ( char )( 'a' + j);
return key;
}
static void wordsWithSameCharSet(String []words, int n)
{
Dictionary<String,
List< int >> Hash = new Dictionary<String,
List< int >>();
for ( int i = 0; i < n; i++)
{
String key = getKey(words[i]);
if (Hash.ContainsKey(key))
{
List< int > get_al = Hash[key];
get_al.Add(i);
Hash[key]= get_al;
}
else
{
List< int > new_al = new List< int >();
new_al.Add(i);
Hash.Add(key, new_al);
}
}
foreach (KeyValuePair<String, List< int >> it in Hash)
{
List< int > get =it.Value;
foreach ( int v in get )
Console.Write( words[v] + ", " );
Console.WriteLine();
}
}
public static void Main(String []args)
{
String []words = { "may" , "student" , "students" ,
"dog" , "studentssess" , "god" ,
"cat" , "act" , "tab" ,
"bat" , "flow" , "wolf" ,
"lambs" , "amy" , "yam" ,
"balms" , "looped" , "poodle" };
int n = words.Length;
wordsWithSameCharSet(words, n);
}
}
|
Javascript
<script>
const MAX_CHAR = 26;
function getKey(str) {
let visited = new Array(MAX_CHAR).fill( false );
for (let j = 0; j < str.length; j++) {
visited[str.charCodeAt(j) - 97] = true ;
}
let key = "" ;
for (let j = 0; j < MAX_CHAR; j++) {
if (visited[j]) {
key += String.fromCharCode(97 + j);
}
}
return key;
}
function wordsWithSameCharSet(words, n) {
let Hash = {};
for (let i = 0; i < n; i++) {
let key = getKey(words[i]);
if (key in Hash) {
Hash[key].push(i);
} else {
Hash[key] = [i];
}
}
for (let key in Hash) {
for (let v of Hash[key]) {
console.log(words[v] + ", " );
}
console.log( "\n" );
}
}
function main() {
let words = [ "may" , "student" , "students" , "dog" , "studentssess" , "god" , "cat" , "act" , "tab" , "bat" , "flow" , "wolf" , "lambs" , "amy" , "yam" , "balms" , "looped" , "poodle" , ];
let n = words.length;
wordsWithSameCharSet(words, n);
}
main();
</script>
|
Output
looped, poodle,
student, students, studentssess,
may, amy, yam,
dog, god,
cat, act,
tab, bat,
lambs, balms,
flow, wolf,
Time Complexity: O(n*k) where n is number of words in dictionary and k is maximum length of a word.
Auxiliary Space: O(n*k), where n is number of words in dictionary and k is maximum length of a word.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!