题解 | #单词拆分(一)#
单词拆分(一)
http://www.nowcoder.com/practice/c0d32c1ce5744472a01b2351a2c2767f
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param dic string字符串一维数组
* @return bool布尔型
*/
public boolean wordDiv (String s, String[] dic) {
List<String> list = Arrays.asList(dic);
HashSet<String> set = new HashSet<>(list);
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && set.contains(s.substring(j, i))){
dp[i] = true;
}
}
}
return dp[s.length()];
}
}