We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 025474c commit b1e9494Copy full SHA for b1e9494
lcci/17.13.Re-Space/Solution.java
@@ -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