Skip to main content

· 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

https://leetcode.com/problems/fizz-buzz/

Solution 1

/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function (n) {
const result = [];

for (let i = 1; i <= n; i++) {
if (i % 15 === 0) {
result.push('FizzBuzz');
} else if (i % 5 === 0) {
result.push('Buzz');
} else if (i % 3 === 0) {
result.push('Fizz');
} else {
result.push(i.toString());
}
}

return result;
};

Runtime: 102 ms, faster than 64.62% of JavaScript online submissions for Fizz Buzz.

Memory Usage: 44.3 MB, less than 69.20% of JavaScript online submissions for Fizz Buzz.

· One min read

https://leetcode.com/problems/number-of-segments-in-a-string/

/**
* @param {string} s
* @return {number}
*/
var countSegments = function (s) {
if (s.length === 0) {
return 0;
}

let result = s[0] !== ' ' ? 1 : 0;

for (let i = 0; i < s.length; i++) {
if (s[i - 1] === ' ' && s[i] !== ' ') {
result++;
}
}

return result;
};

Runtime: 77 ms, faster than 76.86% of JavaScript online submissions for Number of Segments in a String.

Memory Usage: 42 MB, less than 30.33% of JavaScript online submissions for Number of Segments in a String.

· One min read

https://leetcode.com/problems/repeated-substring-pattern/

/**
* @param {string} s
* @return {boolean}
*/
var repeatedSubstringPattern = function (s) {
// s를 두 번 반복한 후, 첫 글자, 맨 마지막 글자를 제거한 후 그 안에 s가 들어있는지 판별
return s.repeat(2).slice(1, -1).includes(s);
};

Runtime: 106 ms, faster than 61.11% of JavaScript online submissions for Repeated Substring Pattern.

Memory Usage: 45 MB, less than 60.32% of JavaScript online submissions for Repeated Substring Pattern.

· 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
/**
* @param {string} word
* @return {boolean}
*/
var detectCapitalUse = function (word) {
if (word.length === 1) {
return true;
}

const isAllLowerCase = (word) => {
return word.charCodeAt(0) >= 97 && word.charCodeAt(0) <= 122;
};
const isAllUpperCase = (word) => {
return word.charCodeAt(0) >= 65 && word.charCodeAt(0) <= 90;
};

if (isAllLowerCase(word[0])) {
for (let i = 0; i < word.length; i++) {
if (!isAllLowerCase(word[i])) {
return false;
}
}
} else {
if (isAllLowerCase(word[1])) {
for (let i = 1; i < word.length; i++) {
if (!isAllLowerCase(word[i])) {
return false;
}
}
} else {
for (let i = 0; i < word.length; i++) {
if (!isAllUpperCase(word[i])) {
return false;
}
}
}
}
return true;
};

Runtime: 97 ms, faster than 63.01% of JavaScript online submissions for Detect Capital.

Memory Usage: 42.4 MB, less than 38.36% of JavaScript online submissions for Detect Capital.

· One min read

https://leetcode.com/problems/same-tree/

Solution 1

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
if (p === null && q === null) {
return true;
}

if ((p !== null && q === null) || (p === null && q !== null)) {
return false;
}

if (p.val !== q.val) {
return false;
}

return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};

· One min read

https://leetcode.com/problems/symmetric-tree/

Solution 1

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function (root) {
return root === null || isMirrorValue(root.left, root.right);
};

const isMirrorValue = (left, right) => {
if (left === null && right === null) {
return true;
}
if (left === null || right === null) {
return false;
}

if (left.val === right.val) {
return (
isMirrorValue(left.right, right.left) &&
isMirrorValue(left.left, right.right)
);
} else {
return false;
}
};

· One min read

https://leetcode.com/problems/maximum-depth-of-binary-tree/

Solution 1

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function (root) {
if (root === null) {
return 0;
}

return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
};

· One min read

https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/

Solution 1

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = function (nums) {
if (nums.length === 0) {
return null;
}
return createBST(nums, 0, nums.length - 1);
};

const createBST = (nums, low, high) => {
if (low > high) {
return null;
}

let mid = Math.floor((high + low) / 2);

let node = new TreeNode(nums[mid]);
node.left = createBST(nums, low, mid - 1);
node.right = createBST(nums, mid + 1, high);
return node;
};