알고리즘 기초 다지기 프로젝트 (feat. 코드없는 프로그래밍) [2021년 05월 28일]
Leetcode - Valid Palindrome
문제: LeetCode - 125. Valid Palindrome
코드
const isPalindrome = (s) => {
if (!s) return true;
s = s
.replace(
/[^a-zA-z\d]|[\{\}\[\]\/?.,;:|\)*~`!^\-+<>@\#$%&\\\=\(\'\"\_]/g,
'',
)
.toLowerCase();
let left = 0;
let right = s.length - 1;
while (left < right) {
const strLeft = s[left];
const strRight = s[right];
if (strLeft === strRight) {
left++;
right--;
} else return false;
}
return true;
};
Leetcode - Valid Palindrome II
문제: LeetCode - 680. Valid Palindrome II
코드
const validPalindrome = (s) => {
if (!s) return true;
s = s
.replace(
/[^a-zA-z\d]|[\{\}\[\]\/?.,;:|\)*~`!^\-+<>@\#$%&\\\=\(\'\"\_]/g,
'',
)
.toLowerCase();
let left = 0;
let right = s.length - 1;
while (left < right) {
const strLeft = s[left];
const strRight = s[right];
if (strLeft !== strRight)
return (
oneMoreCheck(s, left + 1, right) ||
oneMoreCheck(s, left, right - 1)
);
left++;
right--;
}
return true;
};
const oneMoreCheck = (s, left, right) => {
while (left < right) {
const strLeft = s[left];
const strRight = s[right];
if (strLeft !== strRight) return false;
left++;
right--;
}
return true;
};
참고 자료
강의
알고리즘 기초 다지기 프로젝트 (feat. 코드없는 프로그래밍) [2021년 05월 28일]
Leetcode - Valid Palindrome
문제: LeetCode - 125. Valid Palindrome
코드
Leetcode - Valid Palindrome II
문제: LeetCode - 680. Valid Palindrome II
코드
참고 자료
강의