Minimum Number of Moves to Make Palindrome LeetCode solution in Python
1class Solution(object):2def minMovesToMakePalindrome(self, s):3"""4:type s: str5:rtype: int6"""7count, length_of_s = 0, len(s)8if length_of_s <= 2:9return count10for i in reversed(range(length_of_s)):11if s[i] != s[0]:12continue13if i == 0:14# move to middle is a speical case which takes len(s)/2 moves then do recursive for remaining part15count += len(s) / 2 + self.minMovesToMakePalindrome(s[1:])16else:17# this move takes len(s)-1 - i (move from i to last index len(s)-1)and then do recursive for remaining part18count += (19len(s) - 1 - i + self.minMovesToMakePalindrome(s[1:i] + s[i + 1 :])20)21break22return count