๐ 139. ๅ่ฏๆๅ
2022ๅนด6ๆ9ๆฅๅฐไบ 1 ๅ้
๐ 139. ๅ่ฏๆๅ
้พๅบฆ: ๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
// ๆ่ทฏ๏ผ
// dp[i] = dp[i - [j]]
int len = wordDict.size();
int amount = s.length();
boolean[] dp = new boolean[amount + 1];
dp[0] = true;
for(int i = 1; i <= amount; i ++) {
for(int j = 0; j < len; j ++) {
// ๅ่ฎพ้่ฆ [j] ๅค็ๅ่ฏ๏ผๅ ไปฅ i ไธบๆๆซไธๆ ๏ผๅพๅๆจ่ฟไธชๅ่ฏๅฟ
้กปๅน้
// ไธ้คๅป่ฟไธชๅ่ฏ็ๅ้ข้จๅไนๅฟ
้กปๅน้
String cur = wordDict.get(j);
int size = cur.length();
if(i - size >= 0 && dp[i - size]) {
if(mySol(s, i - size, cur)) {
dp[i] = true;
}
}
}
}
return dp[amount] == true;
}
private boolean mySol(String s, int from, String cur) {
int index = from;
int len = cur.length();
for(int i = 0; i < len; i ++) {
if(s.charAt(index + i) != cur.charAt(i)) {
return false;
}
}
return true;
}
}