Similar Problems

Similar Problems not available

Minimum Moves To Convert String - Leetcode Solution

Companies:

LeetCode:  Minimum Moves To Convert String Leetcode Solution

Difficulty: Easy

Topics: greedy string  

Problem Statement:

Given a string s consisting of n lowercase letters, you have to convert s into a palindrome. You are allowed to perform the following two operations on s:

  1. Replace one letter of s with a different letter.
  2. Delete one letter from s.

Return the minimum number of moves required to convert s into a palindrome.

A palindrome is a string that reads the same backward as forward.

Example:

Input: s = "abcd" Output: 2 Explanation: You can convert "abcd" to "abba" with two operations: replace 'c' with 'b' and delete 'd'.

Solution:

Let's first discuss the brute force approach for solving this problem. For each character in the string 's', we can try either deleting it or changing it to its corresponding character on the opposite side of the string to get a palindrome. We will keep track of the minimum number of moves we need to make for this conversion. We will try all possible combinations of deleting or swapping characters in the string until we find the minimum number of moves required.

But this approach will take a lot of time because there can be thousands of possible combinations to try. Therefore, we need to use dynamic programming to make this solution efficient.

We will define a 2D array 'dp' of size n x n, where dp[i][j] represents the minimum number of moves needed to convert the substring s[i:j] into a palindrome.

The base case is when the length of the substring is 1, i.e. dp[i][i] = 0. This is because a single character is already a palindrome and does not require any moves.

The recursive case is defined as follows: If s[i] == s[j], then dp[i][j] = dp[i+1][j-1]. This means that if the characters at the current start and end indexes are the same, then we do not need to make any moves to get a palindrome. Therefore, we can recursively check if the substring s[i+1:j-1] is already a palindrome or not.

If s[i] != s[j], then we have two options:

  1. Delete the character at the start index i, i.e. dp[i][j] = dp[i+1][j] + 1.
  2. Delete the character at the end index j, i.e. dp[i][j] = dp[i][j-1] + 1.

In the above two cases, we are deleting one of the characters to make the substring a palindrome. Therefore, we add 1 to the minimum number of moves needed to get a palindrome for substring s[i+1:j] or s[i:j-1].

The final answer is stored in dp[0][n-1], i.e. the minimum number of moves needed to get a palindrome for the entire string.

Let's see the implementation of this solution in Python:

def minMovesToPalindrome(s: str) -> int: n = len(s)

# initialize dp matrix
dp = [[0]*n for _ in range(n)]

# base case
for i in range(n):
    dp[i][i] = 0

# recursive case
for l in range(2, n+1):
    for i in range(n-l+1):
        j = i+l-1
        if s[i] == s[j]:
            # no moves required
            dp[i][j] = dp[i+1][j-1]
        else:
            # delete character at start
            deleteStart = dp[i+1][j] + 1
            # delete character at end
            deleteEnd = dp[i][j-1] + 1
            dp[i][j] = min(deleteStart, deleteEnd)

return dp[0][n-1]

testing the function

print(minMovesToPalindrome("abcd")) # output: 2

Time complexity: O(n^2), where n is the length of the string 's'. Space complexity: O(n^2), for storing the dp matrix.

Minimum Moves To Convert String Solution Code

1