Meta interview question

How many unique world are in the provided string?

Interview Answers

Anonymous

6 Apr 2017

import java.util.HashSet; import java.util.Set; public class UniqueWordsInString{ public static void main(String str[]){ countUniqueWords("Hello john Hello cena Hello mik"); } public static void countUniqueWords(String s){ Set uniqueWords=new HashSet(); String [] words=s.split("\\W+"); for(String word: words){ uniqueWords.add(word); } System.out.println("Unique words"+uniqueWords.toString()); } }

Anonymous

8 Apr 2017

Thanks

Anonymous

20 Apr 2017

I think this needs backtracking & exhaustive search. Start from the left pick the first letter, next pick candidate second letter. In this case any of the 2nd to the last letter is a candidate. Now with two letters we have 3 possibilities: 1. A matching two letter is found in dictionary. In this case increment word count. 2. No words in the dictionary begin with these two letters. In this case dump this pair of letters for further exploration. (Prune tree in backtracking+exhaustive search parlance) 3. Multiple words begin with these first two letter. In which case we will continue exploring this path, by adding a candidate 3rd letter. And so on.

Anonymous

3 Sept 2018

len(set(re.sub('[' + string.punctuation + ']', '', sentence.lower()).split()))