Adi S
Adi S
Jan 18 · 1 min read

These are the answers I have for the questions.

1.

const solution = (str) => {
if(str.length <= 2) {
return str;
}

let result = str.slice(0, 2);

for(let i = 2; i < str.length; i++) {
if(str[i] === str[i-2] && str[i] === str[i-1]) {
continue;
}
result += str[i];
}

return result;
}

2.

class Solution {

getDigitSum(num) {
let res = 0;
while (num >= 1) {
res += Math.floor(num % 10);
num /= 10;
}
return res;
}

maxSum(A) {
let n = A.length;

if (A === null || n <= 1) {
return -1;
}

let sumMap = {};
let res = -1;

for (let num of A) {
let digitSum = this.getDigitSum(num);

if (!sumMap.hasOwnProperty(digitSum)) {
sumMap[digitSum] = num;
}
else {
res = Math.max(res, sumMap[digitSum] + num);

sumMap[digitSum] = Math.max(sumMap[digitSum], num);
}
}
return res;
}
}

let solution = new Solution();
console.log(solution.maxSum([51, 71, 17, 42])); // expected: 93
console.log(solution.maxSum([42, 33, 60])); // expected: 102
console.log(solution.maxSum([51, 32, 43])); // expected: -1

3.

class Solution {

minMoves(s) {
// Time Complexity: O(n)
// Space Complexity: O(1)

if (s === null || s.length < 3) {
return 0;
}

let res = 0;
for (let i = 0; i < s.length;) {
let next = i + 1;
while (next < s.length && s[i] === s[next]) {
next++;
}
res += Math.floor((next - i)/3);
i = next;
}
return res;
}
}

let solution = new Solution();
console.log(solution.minMoves("baaaaa")); // expected: 1
console.log(solution.minMoves("baaabbaabbba")); // expected: 2
console.log(solution.minMoves("baabab")); // expected: 0
console.log(solution.minMoves("aaaaaaa")); // expected: 2
console.log(solution.minMoves("aaabb")); // expected: 1

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade