1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public int strangePrinter(String s) { int n = s.length(); int[][] f = new int[n + 1][n + 1]; for (int len = 1; len <= n; ++len) { for (int l = 0; l + len - 1 < n; ++l) { int r = l + len - 1; f[l][r] = f[l + 1][r] + 1; for (int k = l + 1; k <= r; ++k) { if (s.charAt(l) == s.charAt(k)) { f[l][r] = Math.min(f[l][r], f[l][k - 1] + f[k + 1][r]); } } } } return f[0][n - 1]; } }
|