Leetcode: 2193. Minimum Number of Moves to Make Palindrome
- Mistake: Overcomplicated solution. Tried to search through all possible sub-problems and didn't consider (couldn't prove) that the greedy approach work.
- Can we break-down the problem in small and easily to solve parts? If the first and last char match, we can discard them. Otherwise, we should fix the end that requires the less amount of swaps. Time complexity is \(O(n^2)\) and space is \(O(n)\).
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