[LeetCode] 2697. Lexicographically Smallest Palindrome

Chobby·3일 전

LeetCode

목록 보기
783/800

😎풀이

  1. s를 배열 형태로 분리
  2. 투 포인터 순회
    2-1. 더 작은 문자로 양 극의 문자를 동기화
  3. 최종 문자열 취합하여 사전순으로 가장 작은 펠린드롬 반환
function makeSmallestPalindrome(s: string): string {
    const splitted = [...s]
    let left = 0
    let right = s.length - 1
    while(left < right) {
        const l = s[left]
        const r = s[right]
        if(l > r) {
            splitted[left] = r
        } else if(r >= l) {
            splitted[left] = l
            splitted[right] = l
        }
        left++
        right--
    }
    return splitted.join('')
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글