Wednesday, February 12, 2014

Dynamic Programming - Find words in Dictionary


Given an input string and a dictionary of words, find out if the input string
can be segmented into a space-separated sequence of dictionary words. See following examples for more details.
This is a famous Google interview question, also being asked by many other companies.

Examaple:

Consider the following dictionary
{ i, like, sam, sung, samsung, mobile, ice,
cream, icecream, man, go, mango}

Input:  ilike
Output: Yes
The string can be segmented as "i like".

Input:  ilikesamsung
Output: Yes
The string can be segmented as "i like samsung" or "i like sam sung".

Codes in java:
public class Find_words_in_dictionary {

    //compare with dictionary by word
     public boolean dictionaryContains(String word) {
  
         String[] dict = {"mobile", "samsung", "sam", "sung", "man", "mango",
                          "icecream", "and", "go", "i", "like", "ice", "cream"};
         int size = dict.length;
         for (int i = 0; i < size; i++) {
             if (dict[i].compareTo(word) == 0) {
             return true;
             }
         }
         return false;
     }

     // Break the str into words
     @SuppressWarnings("unchecked")
     public boolean breakWords(String str) {
         if (str == null || str.length() == 0) {
             return true;
         }
         boolean wb[] = new boolean[str.length()];
         @SuppressWarnings("rawtypes")
         // compare str[0, i) and str[i, j)
         HashMap map = new HashMap();

         for (int i = 1; i < str.length(); i++) {
             if (wb[i - 1] == false && dictionaryContains(str.substring(0, i))) {
                 wb[i - 1] = true;
                 map.put(str.substring(0, i), 1);
                 System.out.println("aaaa " + str.substring(0, i));
             }
             for (int j = i; j <= str.length(); j++) {
                 if (wb[j - 1] == false && dictionaryContains(str.substring(i, j))) {
                     wb[j - 1] = true;
                     map.put(str.substring(i, j), 1);
                     System.out.println("bbbb " + str.substring(i, j));
                 }
             }
         }
        
        // check if wb[] contains false
        for (int i = 0; i < str.length(); i++) {
            if (wb[i]) {
                return true;
            }
        }
        return false;
  
 }
 
    public static void main(String[] args) {
        Find_words_in_dictionary f = new Find_words_in_dictionary();
        boolean flag = f.breakWords("likesamsungmancreamice");
        System.out.println(flag);
  
    }
}

The Output:
bbbb i
aaaa like
bbbb sam
bbbb samsung
bbbb man
bbbb cream
bbbb i
bbbb ice
true

No comments:

Post a Comment