Skip to main content

· One min read

https://leetcode.com/problems/reverse-only-letters/description/

/**
* @param {string} s
* @return {string}
*/
var reverseOnlyLetters = function (s) {
if (s.length === 1) {
return s;
}

let result = s.split('');
let leftIndex = 0;
let rightIndex = s.length - 1;

while (leftIndex <= rightIndex) {
const isLeftEngString = isEngString(s[leftIndex]);
const isRightEngString = isEngString(s[rightIndex]);

if (isLeftEngString && isRightEngString) {
result[leftIndex] = s[rightIndex];
result[rightIndex] = s[leftIndex];
leftIndex++;
rightIndex--;
} else {
if (!isLeftEngString) {
leftIndex++;
}
if (!isRightEngString) {
rightIndex--;
}
}
}
return result.join('');
};

const isEngString = (letter) => {
const engLettersRegex = /[a-zA-Z]/;
return engLettersRegex.test(letter);
};

Runtime 59 ms Beats 97.4%

Memory 42.6 MB Beats 30.37%

· One min read

https://leetcode.com/problems/shortest-distance-to-a-character/description/

Solution 1.

/**
* @param {string} s
* @param {character} c
* @return {number[]}
*/
var shortestToChar = function (s, c) {
const targets = [];
for (let i = 0; i < s.length; i++) {
if (s[i] === c) {
targets.push(i);
}
}

let targetIndex = 0;
const result = [];
for (let j = 0; j < s.length; j++) {
const nextIndex = targetIndex + 1;
if (!isNaN(targets[nextIndex])) {
result.push(
Math.min(
Math.abs(j - targets[targetIndex]),
Math.abs(j - targets[nextIndex]),
),
);

if (j - targets[nextIndex] === 0) {
targetIndex++;
}
} else {
if (s[j] === c) {
result.push(0);
} else {
result.push(Math.abs(j - targets[targetIndex]));
}
}
}

return result;
};

Runtime 131 ms Beats 21.28%

Memory 44.5 MB Beats 54.4%

· One min read

https://leetcode.com/problems/unique-morse-code-words/description/

Solution 1.

/**
* @param {string[]} words
* @return {number}
*/
var uniqueMorseRepresentations = function (words) {
if (words.length === 1) {
return 1;
}

const wordMorse = [];

let currentMorse = '';
for (const word of words) {
currentMorse = '';
for (let i = 0; i < word.length; i++) {
currentMorse += morse[word[i].charCodeAt(0) - 97];
}
wordMorse.push(currentMorse);
}

return new Set(wordMorse).size;
};

const morse = [
'.-',
'-...',
'-.-.',
'-..',
'.',
'..-.',
'--.',
'....',
'..',
'.---',
'-.-',
'.-..',
'--',
'-.',
'---',
'.--.',
'--.-',
'.-.',
'...',
'-',
'..-',
'...-',
'.--',
'-..-',
'-.--',
'--..',
];

Runtime 103 ms Beats 50.94%

Memory 42.9 MB Beats 82.45%

· One min read

https://leetcode.com/problems/move-zeroes/description/

Solution 1

/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function (nums) {
let zeroCount = 0;

for (let i = 0; i < nums.length; i++) {
if (nums[i] !== 0) {
const zero = nums[zeroCount];
nums[zeroCount] = nums[i];
nums[i] = zero;
zeroCount++;
}
}

return nums;
};

Runtime 147 ms Beats 64.1%

Memory 47 MB Beats 27.90%

· One min read

https://leetcode.com/problems/jewels-and-stones/

/**
* @param {string} jewels
* @param {string} stones
* @return {number}
*/
var numJewelsInStones = function (jewels, stones) {
let result = 0;
for (let i = 0; i < stones.length; i++) {
if (jewels.includes(stones[i])) {
result++;
}
}
return result;
};

Runtime: 95 ms, faster than 62.67% of JavaScript online submissions for Jewels and Stones.

Memory Usage: 41.9 MB, less than 93.79% of JavaScript online submissions for Jewels and Stones.

· One min read

https://leetcode.com/problems/minimum-index-sum-of-two-lists/

/**
* @param {string[]} list1
* @param {string[]} list2
* @return {string[]}
*/
var findRestaurant = function (list1, list2) {
const hashTable = {};

list1.forEach((el, index) => {
const indexOfEl2 = list2.findIndex((el2) => el2 === el);
if (indexOfEl2 !== -1) {
hashTable[el] = indexOfEl2 + index;
}
});

const size = Object.keys(hashTable).length;
if (size === 0) {
return [];
}

const minIndexSum = Math.min(
...Object.values(hashTable).map((value) => value),
);

return Object.keys(hashTable).filter((key) => hashTable[key] === minIndexSum);
};

Runtime: 309 ms, faster than 13.28% of JavaScript online submissions for Minimum Index Sum of Two Lists.

Memory Usage: 49.2 MB, less than 54.39% of JavaScript online submissions for Minimum Index Sum of Two Lists.

· One min read

https://leetcode.com/problems/rotate-string/

Solution 1

/**
* @param {string} s
* @param {string} goal
* @return {boolean}
*/
var rotateString = function (s, goal) {
if (s.length !== goal.length) {
return false;
}

let rearrangeS = s;
for (let i = 0; i < s.length; i++) {
rearrangeS = rearrangeS.slice(1) + rearrangeS.slice(0, 1);
if (rearrangeS === goal) {
return true;
}
}
return false;
};

Runtime: 64 ms, faster than 90.15% of JavaScript online submissions for Rotate String.

Memory Usage: 42 MB, less than 66.54% of JavaScript online submissions for Rotate String.

· One min read

https://leetcode.com/problems/most-common-word/

/**
* @param {string} paragraph
* @param {string[]} banned
* @return {string}
*/
var mostCommonWord = function (paragraph, banned) {
const onlyLowerCase = paragraph
.toLowerCase()
.split(/[ !?',;.]/)
.filter((val) => val !== '');
const hashTable = {};

onlyLowerCase.forEach((word) => {
if (!banned.includes(word)) {
hashTable[word] = hashTable[word] + 1 || 1;
}
});

const maxCount = Math.max(...Object.values(hashTable).map((value) => value));

return Object.keys(hashTable).find((key) => hashTable[key] === maxCount);
};

Runtime: 75 ms, faster than 90.68% of JavaScript online submissions for Most Common Word.

Memory Usage: 43.9 MB, less than 82.19% of JavaScript online submissions for Most Common Word.

· One min read

https://leetcode.com/problems/ransom-note/

Solution 1

/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
var canConstruct = function (ransomNote, magazine) {
const splitB = magazine.split('');

for (let i = 0; i < ransomNote.length; i++) {
const matchedIndex = splitB.indexOf(ransomNote[i]);
if (matchedIndex !== -1) {
splitB.splice(matchedIndex, 1);
} else {
return false;
}
}

return true;
};

Runtime: 116 ms, faster than 76.45% of JavaScript online submissions for Ransom Note.

Memory Usage: 46.5 MB, less than 30.65% of JavaScript online submissions for Ransom Note.