Skip to main content

31 posts tagged with "string"

View All Tags

· 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/valid-palindrome/

Solution 1

/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (s) {
const lowerCaseString = s.toLowerCase();
let onlyLowerCase = '';

for (let i = 0; i < lowerCaseString.length; i++) {
const regex = /^[a-z0-9]/;
if (regex.test(lowerCaseString[i])) {
onlyLowerCase += lowerCaseString[i];
}
}

let reverseOnlyLowerCase = '';

for (let j = onlyLowerCase.length - 1; j >= 0; j--) {
reverseOnlyLowerCase += onlyLowerCase[j];
}

console.log(onlyLowerCase, reverseOnlyLowerCase);

return onlyLowerCase === reverseOnlyLowerCase;
};

· 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/longest-common-prefix/

Solution 1

/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function (strs) {
if (strs.length === 1) {
return strs[0];
}

const [baseWord, ...rest] = strs;

for (let i = 0; i <= baseWord.length; i++) {
for (let j = 0; j < strs.length; j++) {
if (baseWord[i] !== strs[j][i]) {
return baseWord.slice(0, i);
}
}
}

return baseWord;
};

Runtime: 116 ms, faster than 25.96% of JavaScript online submissions for Longest Common Prefix.

Memory Usage: 42.6 MB, less than 52.34% of JavaScript online submissions for Longest Common Prefix.

Solution 2

/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function (strs) {
if (!strs.length) {
return '';
}

for (let i = 0; i <= strs[0].length; i++) {
if (!strs.every((string) => string[i] === strs[0][i])) {
return strs[0].slice(0, i);
}
}

return strs[0];
};

Runtime: 85 ms, faster than 77.67% of JavaScript online submissions for Longest Common Prefix.

Memory Usage: 43.1 MB, less than 34.02% of JavaScript online submissions for Longest Common Prefix.

· One min read

https://leetcode.com/problems/excel-sheet-column-title/

Solution 1

/**
* @param {number} columnNumber
* @return {string}
*/
var convertToTitle = function (columnNumber) {
// String.fromCharCode(65) === "A"
// 한 자릿수일 경우
if (columnNumber - 1 < 26) {
return String.fromCharCode(65 + ((columnNumber - 1) % 26));
}

// 두 자릿수 이상일 경우
let result = '';
while (columnNumber > 0) {
const remainder = (columnNumber - 1) % 26;
const remainderCharCode = String.fromCharCode(65 + remainder);
result = remainderCharCode + result;
columnNumber = parseInt((columnNumber - 1) / 26);
}
return result;
};

다시 봐야함

· One min read

https://leetcode.com/problems/excel-sheet-column-number/

Solution 1

/**
* @param {string} columnTitle
* @return {number}
*/
var titleToNumber = function (columnTitle) {
if (columnTitle.length === 1) {
return (columnTitle.charCodeAt(0) % 65) + 1;
}

let result = 0;

for (let i = columnTitle.length - 1; i >= 0; i--) {
const strCode = (columnTitle[i].charCodeAt(0) % 65) + 1;
if (i === columnTitle.length - 1) {
result += strCode;
} else {
result += strCode * 26 ** (columnTitle.length - i - 1);
}
}
return result;
};