bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 2193. Minimum Number of Moves to Make Palindrome

2193. Minimum Number of Moves to Make Palindrome

Leetcode Solutions

Two Pointers Approach to Make String Palindrome

  1. Initialize left pointer to 0 and right pointer to the length of the string minus one.
  2. While left is less than right, do the following: a. If the characters at left and right are the same, increment left and decrement right. b. If they are not the same, find the index k of the character from the right side that matches the character at left. c. If no such k is found (which means the character at left is unique), swap it with the next character and increment the number of moves. d. If k is found, swap characters from k to right - 1 to move the matching character to the right position, incrementing the number of moves for each swap. e. After the swaps, increment left and decrement right.
  3. Return the total number of moves required to make the string a palindrome.
UML Thumbnail

Greedy Approach with Character Deletion and Insertion

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...