forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString Compression II.java
76 lines (67 loc) · 2.49 KB
/
String Compression II.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
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
74
75
76
class Solution {
public int getLengthOfOptimalCompression(String s, int k) {
Map<String, Integer> memo = new HashMap<>();
return recur(s, '\u0000', 0, k, 0, memo);
}
private int recur(String s, char prevChar, int prevCharCount, int k, int index, Map<String, Integer> memo) {
if (index == s.length()) {
return 0;
}
String key = prevChar + ", " + prevCharCount + ", " + k + ", " + index;
Integer keyVal = memo.get(key);
if (keyVal != null) {
return keyVal;
}
char ch = s.charAt(index);
int count = 1;
int nextIndex = index + 1;
for (int i = index + 1; i < s.length(); i++) {
if (s.charAt(i) == ch) {
count++;
nextIndex = i + 1;
} else {
nextIndex = i;
break;
}
}
int totalCount = count;
int prevCountRepresentation = 0;
//if prev char is equal to current char that means we have removed middle element
//So we have to subtract the previous representation length and add the new encoding
//representation length
if (ch == prevChar) {
totalCount += prevCharCount;
prevCountRepresentation = getLength(prevCharCount);
}
int representaionLength = getLength(totalCount);
int ans = representaionLength + recur(s, ch, totalCount, k, nextIndex, memo) - prevCountRepresentation;
if (k > 0) {
for (int i = 1; i <= k && i <= count; i++) {
int currentCount = totalCount - i;
int length = getLength(currentCount);
//checking if we have to send current char and current char count or previous char
//and previous char count
int holder = length + recur(s, currentCount == 0 ? prevChar : ch,
currentCount == 0 ? prevCharCount : currentCount, k - i, nextIndex, memo) -
prevCountRepresentation;
ans = Math.min(ans, holder);
}
}
memo.put(key, ans);
return ans;
}
//Since length for aaaaa will be a5(2) aaaaaaaaaa a10(3) etc.
private int getLength(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else if (n < 10) {
return 2;
} else if (n < 100) {
return 3;
} else {
return 4;
}
}
}