Minimum number of moves to make palindrome

December 25, 2022

two-pointers

Problem URL: Minimum number of moves to make palindrome

We will iterate over the string and count the number of moves for each character. We will update the result.

class Solution:
    def minMovesToMakePalindrome(self, s: str) -> int:
        s = list(s)
        res = 0
        left, right = 0, len(s)-1
        while left < right:
            l, r = left, right
            while s[l] != s[r]:
                r -= 1
            if l == r:
                s[r], s[r + 1] = s[r + 1], s[r]
                res += 1
                continue
            else:
                while r < right:
                    s[r], s[r + 1] = s[r + 1], s[r]
                    res += 1
                    r += 1
            left += 1
            right -= 1
        return res

Time complexity: O(n^2)
Space complexity: O(1)