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: 93console.log(solution.maxSum([42, 33, 60]));  // expected: 102console.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: 1console.log(solution.minMoves("baaabbaabbba"));  // expected: 2console.log(solution.minMoves("baabab"));        // expected: 0console.log(solution.minMoves("aaaaaaa"));       // expected: 2console.log(solution.minMoves("aaabb"));         // expected: 1`

Written by

Jan 29 · 2 min read

#### 164

Feb 25 · 2 min read