2193. Minimum Number of Moves to Make Palindrome

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2193

class Solution:
    def minMovesToMakePalindrome(self, s: str) -> int:
        cs = list(s)
        res, n = 0, len(s)
        i, j = 0, n - 1
        while i < j:
            even = False
            for k in range(j, i, -1):
                if cs[i] == cs[k]:
                    even = True
                    while k < j:
                        cs[k], cs[k + 1] = cs[k + 1], cs[k]
                        k += 1
                        res += 1
                    j -= 1
                    break
            if not even:
                res += n // 2 - i
            i += 1
        return res