본문 바로가기
python-algorithm

leetcode 2697. Lexicographically Smallest Palindrome

by 무적김두칠 2023. 12. 28.

https://leetcode.com/problems/lexicographically-smallest-palindrome/description/

 

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def makeSmallestPalindrome(self, s: str-> str:
        s = list(s)
        for i in range(len(s)//2):
            if s[i] != s[-(i+1)]:
                if s[i] < s[-(i+1)]:
                    s[-(i+1)] = s[i]
                else:
                    s[i] = s[-(i+1)]
        
        return ''.join(s)
cs
반응형

댓글