*** Minimum Number of Moves to Make Palindrome (Leetcode 2193)

Problem Link: https://leetcode.com/problems/minimum-number-of-moves-to-make-palindrome/

class Solution:
    def minMovesToMakePalindrome(self, s: str) -> int:
        
        s=list(s)
        ans=0
        while(s):
            
            # Returns the first occurance
            # of the s[-1](last character)
            i=s.index(s[-1])
            
            # If the count of that char is 1
            if(i==len(s)-1):
                ans=ans+len(s)//2
                s.pop()
                
            # If there are multiple such chars
            else:
                # Number of swaps needed to move
                # a char from ith position to 0th
                # position is i
                ans=ans+i
                s.pop(i)
                s.pop()
        
        return ans

Last updated