import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param wordDict string字符串一维数组
* @return bool布尔型
*/
public boolean wordBreak (String s, String[] wordDict) {
HashSet<String> hashSet = new HashSet<>(Arrays.asList(wordDict));
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] && hashSet.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}
}
本题知识点分析:
1.动态规划
2.API函数
3.数组遍历
4.哈希表去重
本题解题思路分析:
1.利用哈希表将字符串数组先进行去重
2.dp数组代表每个字符串是否被包含在hashSet中
3.dp公式推导 -> dp[j] && hashSet.contains(s.substring(j, i))
4.dp初始化 -> dp[0] = true;
5.dp数组遍历顺序,从前往后
6.如果前面的dp数组为true,并且包含子字符串

京公网安备 11010502036488号