-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2707.java
More file actions
66 lines (53 loc) · 1.73 KB
/
LC2707.java
File metadata and controls
66 lines (53 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* LC2707
*/
import java.util.*;
public class LC2707 {
static int dp[] = new int[50];
public static int recur(String s, HashSet<String> dictionary, int index) {
if (index == s.length()) {
return 0;
}
if (dp[index] != -1) {
return dp[index];
}
StringBuilder sb = new StringBuilder();
int minExtraChar = Integer.MAX_VALUE;
for (int i = index; i < s.length(); i++) {
sb.append(s.charAt(i));
int extraChar = 0;
if (!dictionary.contains(sb.toString())) {
extraChar = sb.length();
}
int currExtra = recur(s, dictionary, i + 1);
minExtraChar = Math.min(minExtraChar, extraChar + currExtra);
}
return dp[index] = minExtraChar;
}
public static int minExtraChar(String s, String[] dictionary) {
int n = s.length();
Arrays.fill(dp, -1);
HashSet<String> dict = new HashSet<>(Arrays.asList(dictionary));
return recur(s, dict, 0);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter String Here : ");
String str = sc.nextLine();
System.out.println();
System.out.print("Enter the Array Size : ");
int size = sc.nextInt();
sc.nextLine();
System.out.println();
String st[] = new String[size];
System.out.println("Enter the String : ");
for (int i = 0; i < st.length; i++) {
System.out.printf("[%d] : ", i);
st[i] = sc.nextLine();
}
System.out.println();
int ans = minExtraChar(str, st);
System.out.println(ans);
sc.close();
}
}