Counting distinct words in a string

Write a Program to input a number of sentences and then count the number of distinct words present in the entered sentences, and then print them alphabetically. Number of sentences will b entered by the user.

check here.

hope it helps.

There are two ways:

1 - Use a map(STL C++)

2 - Use Trie Data Structure.

does this help?

Are you talking about something like this for Distinct word in string?

"""
Sample test case:

Input:
hey hey i am talking to you you

Output:
Distinct words = 4
Words[] = ['i', 'am', 'talking', 'to']
"""

If yes then i just used python dictionary for this problem.

You may see my code over here: CodeSkulptor

Zest of code:

for i in text:
    if store.has_key(i):
        store[i]+=1
    else:
        store[i]=1

text is string

store is dictionary

It is like:

store = { “hey” : 2, “i” : 1, “am” : 1, “talking” : 1, “to” : 1, “you” : 2 }

words represents key

numbers represents value or you may say repetitions.

Hope you understood… :slight_smile:

Just use simple stringstream and set. Check this out. Other solutions may include using Maps and Trie datastructure. Good source for studying Trie: Click this

Explanation: Set helps removing duplicates as it is basically a red black tree.

try {
s = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

    String[] splited = s.split("\\s+");
    
   HashMap<String,Integer> hm= new HashMap<String,Integer>();
    
    for(int i=0;i<splited.length;i++)
    {
   Integer in= 	hm.get(splited[i]);
   if(in==null)
   {hm.put(splited[i], new Integer(0));
	   
	   
   }
   else{
	   in++;
	   hm.put(splited[i], in);  
	   
   }
   
   
   Iterator it = hm.entrySet().iterator();
   while (it.hasNext()) {
       Map.Entry pair = (Map.Entry)it.next();
       System.out.println(pair.getKey() + " = " + pair.getValue());
       it.remove(); // avoids a ConcurrentModificationException
   }
   
    	
    	
    }

thats ok for number of words, but m not able to do for a number of sentences.
i.e to count number of distinct words in more than 1 sentence…
m using a structure in which a charcter array is there n then making an array object so that i can take more than 1 sentence as input. then how to count distinct words in this case?

then use map(C++) or Trie…
trie will be fastest for a very long input file.