
😎풀이
- 접두 & 접미사 체크
- 모두 접두사이자 접미사라면 그대로 반환
- 펠린드롬이 아닌 부분 확인
- 조합 후 반환
function shortestPalindrome(s: string): string {
const length = s.length;
if (!length) return s;
let left = 0;
for (let right = length - 1; right >= 0; right--) {
if (s.charAt(right) === s.charAt(left)) {
left++;
}
}
if (left === length) {
return s;
}
const nonPalindromeSuffix = s.substring(left);
const reverseSuffix = nonPalindromeSuffix.split('').reverse().join('');
return reverseSuffix + shortestPalindrome(s.substring(0, left)) + nonPalindromeSuffix;
};