@@ -0,0 +1,1281 @@
+package org.glamey.training.algorithm.leetcode;%0A%0Aimport com.google.common.collect.Lists;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0A/**%0A * https://leetcode.com/problems/replace-words/description/%0A *%0A * @author zhouyang.zhou. 2017.09.04.15.%0A */%0Apublic class ReplaceWordsDemo %7B%0A%0A  public static void main(String%5B%5D args) %7B%0A    List%3CString%3E dict = Lists.newArrayList(%22cat%22, %22bat%22, %22rat%22);%0A    String sentence = %22the cattle was rattled by the battery%22;%0A    String result = replaceWords(dict, sentence);%0A    System.out.println(result);%0A  %7D%0A%0A  private static String replaceWords(List%3CString%3E dict, String sentence) %7B%0A    if(dict == null %7C%7C dict.size() == 0) %7B%0A      return sentence;%0A    %7D%0A%0A    if(sentence == null %7C%7C %22%22.equals(sentence)) %7B%0A      return sentence;%0A    %7D%0A%0A    StringBuilder builder = new StringBuilder(sentence.length());%0A    Set%3CString%3E dictSet = new HashSet%3C%3E(dict);%0A    String%5B%5D strings = sentence.split(%22%5C%5Cs+%22);%0A    for (String string : strings) %7B%0A      String prefix = %22%22;%0A      for (int i = 1; i %3C= string.length(); i++) %7B%0A        prefix = string.substring(0, i);%0A        if(dictSet.contains(prefix)) %7B%0A          break;%0A        %7D%0A      %7D%0A      builder.append(%22 %22).append(prefix);%0A    %7D%0A    return builder.deleteCharAt(0).toString();%0A  %7D%0A%7D%0A

replace words