-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task2.js
45 lines (42 loc) · 1.13 KB
/
Task2.js
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
function solution(S, K) {
// write your code in JavaScript (Node.js 8.9.4)
let memLen = parseString(S).length;
for (let i = 0; i < S.length - (K - 1); i++) {
let subString = getSubstring(S, i, i + K - 1);
memLen = Math.min(memLen, parseString(subString).length);
}
return memLen;
}
function getSubstring( S, begin,end) {
let sb = "";
for (let i = 0; i < S.length; i++) {
if (i < begin || i > end) {
sb += S[i];
}
}
return sb;
}
function parseString(S) {
let prevChar = S[0], newString = "", count = 1;
for (let index = 1; index < S.length; index++) {
const char = S[index];
if(char === prevChar) {
count++;
} else {
if(count < 2) {
newString += prevChar;
} else {
newString = newString + count + prevChar;
}
count = 1;
prevChar = char;
}
}
if(count < 2) {
newString += prevChar;
} else {
newString = newString + count + prevChar;
}
return newString;
}
console.log(solution("ABBBCCDDCCC", 3));