Skip to main content

16 posts tagged with "hashTable"

View All Tags

· 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/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/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.

· One min read

https://leetcode.com/problems/longest-palindrome/submissions/

/**
* @param {string} s
* @return {number}
*/
var longestPalindrome = function (s) {
let longest = 0;
let keys = {};

for (const char of s) {
keys[char] = (keys[char] || 0) + 1;
if (keys[char] % 2 === 0) {
longest += 2;
}
}
return s.length > longest ? longest + 1 : longest;
};

Runtime: 106 ms, faster than 58.34% of JavaScript online submissions for Longest Palindrome.

Memory Usage: 43.5 MB, less than 73.08% of JavaScript online submissions for Longest Palindrome.

· One min read
/**
* @param {string[]} words
* @return {string[]}
*/
var findWords = function (words) {
const firstRow = 'qwertyuiop';
const secondRow = 'asdfghjkl';
const thirdRow = 'zxcvbnm';

const result = [];

for (const word of words) {
const lowerCaseWord = word.toLocaleLowerCase();
const a = lowerCaseWord.split('').every((word) => firstRow.includes(word));
const b = lowerCaseWord.split('').every((word) => secondRow.includes(word));
const c = lowerCaseWord.split('').every((word) => thirdRow.includes(word));

if (a || b || c) {
result.push(word);
}
}

return result;
};

Runtime: 58 ms, faster than 98.18% of JavaScript online submissions for Keyboard Row.

Memory Usage: 42.1 MB, less than 29.55% of JavaScript online submissions for Keyboard Row.

· One min read

https://leetcode.com/problems/roman-to-integer/

Solution 1

/**
* @param {string} s
* @return {number}
*/
var romanToInt = function (s) {
let total = 0;

for (let i = 0; i < s.length; i++) {
if (values[s[i]] < values[s[i + 1]]) {
total += values[s[i + 1]] - values[s[i]];
i++;
} else {
total += values[s[i]];
}
}
return total;
};

const values = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};

Runtime: 235 ms, faster than 40.69% of JavaScript online submissions for Roman to Integer.

Memory Usage: 47.1 MB, less than 67.53% of JavaScript online submissions for Roman to Integer.

· One min read

https://leetcode.com/problems/linked-list-cycle/

Solution 1

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function (head) {
if (head === null) {
return false;
}

let curr = head;
let next = head.next;

while (next && next.next) {
curr = curr.next;
next = next.next.next;

if (curr === next) {
return true;
}
}

return false;
};

· One min read

https://leetcode.com/problems/intersection-of-two-linked-lists/

Solution 1

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode = function (headA, headB) {
if (!headA || !headB) {
return null;
}

let nodeA = headA;
let nodeB = headB;

while (nodeA !== nodeB) {
nodeA = nodeA.next;
nodeB = nodeB.next;

console.log(nodeA, nodeB);

if (nodeA === nodeB) {
return nodeA;
}

if (!nodeA) {
nodeA = headB;
}
if (!nodeB) {
nodeB = headA;
}
}

return nodeA;
};

· One min read

https://leetcode.com/problems/majority-element/

Solution 1

/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function (nums) {
const element = {};

for (const num of nums) {
if (element[num]) {
element[num]++;
} else {
element[num] = 1;
}
}

let max = 0;
let targetNum = '';

for (let e in element) {
if (element[e] > max) {
max = element[e];
targetNum = e;
}
}

return Number(targetNum);
};