We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 8077f35 + 2e5ad96 commit f685222Copy full SHA for f685222
java/2707-extra-characters-in-a-string.java
@@ -0,0 +1,26 @@
1
+/*
2
+ Let N = length of string s, M = length of dictionary
3
+ Time: O(N * M)
4
+ Space: O(N)
5
+*/
6
+class Solution {
7
+ public int minExtraChar(String s, String[] dictionary) {
8
+ int n = s.length();
9
+ int[] dp = new int[n+1];
10
+
11
+ Arrays.fill(dp, n);
12
+ dp[0] = 0;
13
14
+ for (int i = 1; i <= n; ++i) {
15
+ for (int j = 0; j < dictionary.length; ++j) {
16
+ int len = dictionary[j].length();
17
+ if (i >= len && s.substring(i - len, i).equals(dictionary[j])) {
18
+ dp[i] = Math.min(dp[i], dp[i - len]);
19
+ }
20
21
+ dp[i] = Math.min(dp[i], dp[i - 1] + 1);
22
23
24
+ return dp[n];
25
26
+}
0 commit comments