forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
27 lines (25 loc) · 840 Bytes
/
Solution.java
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
class Solution {
private Integer[][][] f;
private String[] words;
private int n;
public int minimizeConcatenatedLength(String[] words) {
n = words.length;
this.words = words;
f = new Integer[n][26][26];
return words[0].length()
+ dfs(1, words[0].charAt(0) - 'a', words[0].charAt(words[0].length() - 1) - 'a');
}
private int dfs(int i, int a, int b) {
if (i >= n) {
return 0;
}
if (f[i][a][b] != null) {
return f[i][a][b];
}
String s = words[i];
int m = s.length();
int x = dfs(i + 1, a, s.charAt(m - 1) - 'a') - (s.charAt(0) - 'a' == b ? 1 : 0);
int y = dfs(i + 1, s.charAt(0) - 'a', b) - (s.charAt(m - 1) - 'a' == a ? 1 : 0);
return f[i][a][b] = m + Math.min(x, y);
}
}