Minimum Number of Moves to Make Palindrome - Problem

You are given a string s consisting only of lowercase English letters. Your task is to transform this string into a palindrome using the minimum number of adjacent swaps.

In one move, you can select any two adjacent characters of s and swap them. For example, in the string "abc", you can swap 'a' and 'b' to get "bac", or swap 'b' and 'c' to get "acb".

Goal: Return the minimum number of moves needed to make s a palindrome.

Note: The input will always be generated such that s can be converted to a palindrome (meaning each character appears an even number of times, or at most one character appears an odd number of times).

Example: For s = "aab", we need 2 moves: "aab" โ†’ "aba" (swap positions 1 and 2) requires 1 move, making it a palindrome with 1 total move.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "aab"
โ€บ Output: 1
๐Ÿ’ก Note: We can make "aab" a palindrome with 1 move: swap positions 1 and 2 to get "aba". This is a palindrome, so minimum moves = 1.
example_2.py โ€” Multiple Swaps
$ Input: s = "letelt"
โ€บ Output: 2
๐Ÿ’ก Note: Starting with "letelt": First, we need to match 'l' at position 0 with 'l' at position 4. We bubble the 'l' from position 4 to position 5 with 1 swap: "letelt" โ†’ "letlte". Then match the remaining characters. Total: 2 moves.
example_3.py โ€” Already Palindrome
$ Input: s = "abccba"
โ€บ Output: 0
๐Ÿ’ก Note: The string "abccba" is already a palindrome, so no moves are needed. Return 0.

Constraints

  • 1 โ‰ค s.length โ‰ค 2000
  • s consists only of lowercase English letters
  • s can always be converted to a palindrome (guaranteed by problem statement)

Visualization

Tap to expand
Mirror Reflection StrategyCreating a palindrome is like arranging tiles in a mirror patternMirror LineaabcbaMatch 'a' with 'a'Perfect Mirror!Greedy Process:1. Position pointers at string ends2. Find closest match for left character3. Bubble match to correct position4. Move pointers inward, repeatComplexity Analysis:โฑ๏ธ Time: O(nยฒ) - nested loops๐Ÿ’พ Space: O(1) - constant extra spaceโœ… Optimal: Greedy choice is always best๐ŸŽฏ Key InsightMoving the closest matching character minimizes total swapsbecause it reduces the distance other characters need to travel
Understanding the Visualization
1
Start from Edges
Place pointers at both ends of the string, like focusing on the outer edges of a mirror
2
Find Matching Pair
For the left character, find its matching pair from the right side - this will be its mirror reflection
3
Bubble to Position
Use adjacent swaps to move the matching character to the correct mirror position
4
Move Inward
Move both pointers toward the center and repeat until the entire string forms a perfect mirror
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach of always moving the closest matching character to its mirror position is optimal because it minimizes the total number of adjacent swaps needed.
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
28.5K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen