Skip to content

Commit b1e9494

Browse files
committed
feat:add 17.13 Solution for Java
1 parent 025474c commit b1e9494

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lcci/17.13.Re-Space/Solution.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int respace(String[] dictionary, String sentence) {
3+
Set<String> set = new HashSet<>(dictionary.length);
4+
set.addAll(Arrays.asList(dictionary));
5+
6+
int[] dp = new int[sentence.length() + 1];
7+
for (int i = 1; i <= sentence.length(); i++) {
8+
dp[i] = dp[i - 1] + 1;
9+
for (int j = 0;j < i;j++) {
10+
if (set.contains(sentence.substring(j, i))) {
11+
dp[i] = Math.min(dp[i], dp[j]);
12+
}
13+
}
14+
}
15+
return dp[sentence.length()];
16+
}
17+
}

0 commit comments

Comments
 (0)