Give a word a dictionary, each time you delete any letter in the word until one letter is left, forming a sequence, such as Office->offce->ofce->ofc->oc->c. Ask if there is a sequence in the dictionary
1 Packagecheckdictexistsequence;2 ImportJava.util.*;3 4 Public classSolution {5Hashset<string> dict =NewHashset<string>();6 7 Publicstring Check (string[] arr, string word) {8 for(String Str:arr)9 Dict.add (str);Ten if(Checkseq (NewStringBuffer (word)))return"True"; One return"False"; A } - - Public Booleancheckseq (StringBuffer sb) { the if(sb.length () = = 1)return true; - for(inti=0; I<=sb.length ()-1; i++) { - CharCur =Sb.charat (i); - Sb.deletecharat (i); + if(Dict.contains (sb.tostring ())) { - if(Checkseq (SB))return true; + } A Sb.insert (i, cur); at } - return false; - } - - - /** in * @paramargs - */ to Public Static voidMain (string[] args) { + //TODO auto-generated Method Stub -Solution Sol =Newsolution (); theSystem.out.println (Sol.check (Newstring[]{"Offce", "OFCE", "OFC", "OC", "C"}, "office")); * } $ Panax Notoginseng}
G-Face via Prepare:search word delete sequence in dictionary