-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
73 lines (64 loc) · 1.88 KB
/
Solution.cpp
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
67
68
69
70
71
72
73
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
private:
string removeConsecutive(string const& s) {
string result;
int i = 0;
while (i < s.length()) {
char c = s[i++];
result += c;
// Skip consecutive occurences
while (i < s.length() && s[i] == c) {
++i;
}
}
return result;
}
public:
int strangePrinter(string s) {
// if all contiguous, then only one turn is required to print out that
// sequence.
s = removeConsecutive(s);
int n = s.length();
// dp[i][j] represents the number of turns required to print the
// substring s[i:j], after removing consecutive sequences of
// characters.
// The optimal substructure can be defined as
// dp[i][j] = min(
// min(dp[i][k], dp[k + 1][j]) for all i <= k < j,
// dp[i][j - 1] if s[i] == s[j]
// )
vector<vector<int>> dp(n, vector<int>(n, 0));
// Base case: Substring of length 1
for (int i = 0; i < n; ++i) {
// Printing a character/sequence of same characters take 1 turn
dp[i][i] = 1;
}
// Consider increasing lengths with different starting indices
for (int len = 2; len <= n; ++len) {
for (int i = 0; i + len - 1 < n; ++i) {
int j = i + len - 1;
// Start with the maximum possible length
dp[i][j] = len;
// For all possible splits between i and j
for (int k = i; k < j; ++k) {
int turns = dp[i][k] + dp[k + 1][j];
// If the characters at i and j / start and end of the substring
// matches, an extra turn can be saved
dp[i][j] = min(dp[i][j], s[i] == s[j] ? turns - 1 : turns);
}
}
}
return dp[0][n - 1];
}
};