Leetcode: 2193. Minimum Number of Moves to Make Palindrome

Problem Statement

class Solution:
    def minMovesToMakePalindrome(self, s: str) -> int:
        def normalize(s):
            i = 0
            j = len(s) - 1
            while i < j:
                if s[i] != s[j]:
                    return s[i : j + 1]
                i += 1
                j -= 1
            return ""

        ans = 0
        s = normalize(s)
        while s:
            i = s.find(s[-1])
            j = s.rfind(s[0])
            if i < len(s) - j - 1:
                ans += i
                s = normalize(s[-1] + s[:i] + s[i + 1 :])
            else:
                ans += len(s) - j - 1
                s = normalize(s[:j] + s[j + 1 :] + s[0])
        return ans