Question d’entretien chez Meta

How many unique world are in the provided string?

Réponses aux questions d'entretien

Utilisateur anonyme

6 avr. 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()); } }

Utilisateur anonyme

8 avr. 2017

Thanks

Utilisateur anonyme

20 avr. 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.

Utilisateur anonyme

3 sept. 2018

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