Minimizing Operations to Rewrite a String
Last Updated :
28 Nov, 2023
Given a string S, you have to write the same given string using two operations, the task is to find the minimum number of operations to write it.
- You can only write a sequence of the same character each time.
- You can choose any substring and overwrite the existing characters with single new characters.
Examples:
Input: s = “aaaddd”
Output: 2
Explanation: Write “aaa” first and then write “ddd”.
Input: s = “aca”
Output: 2
Explanation: Write “aaa” first and then write “c” from the second place of the string, which will cover the existing character ‘a’.
Minimizing Operations to Rewrite a String using Dynamic Programming.
This problem is easier to solve if we reduce it to subproblems using Divide and Conquer. We can break it down into smaller subproblems, we keep dividing s until the substring contains 1 or 2 characters (as the base case). We’ll define a DP table dp, where dp[i][j] will represent the minimum number of operations needed to write the substring s[i…j] (inclusive).
Base Case:For a single character (substring of length 1), we need only one operation to write it. Therefore, dp[i][i] = 1 for all i.
Recursive Formula: Now, we need to find the minimum number of operations for substrings of length 2, 3, and so on, up to the entire string length.
- For each substring length, we’ll iterate over all possible starting points (i) and ending points (j) of that substring.
- To compute dp[i][j], we’ll iterate over all possible k from i to j-1 (inclusive).
- For each k, we’ll try to find the minimum number of operations by combining the minimum operations required to write s[i…k] and s[k+1…j].
Final Answer: The answer to the problem will be stored in dp[0][n-1], where n is the length of the string.
Below is the implementation of the above approach:
C++
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
int writeString(string s)
{
int n = s.length();
vector<vector< int > > dp(n, vector< int >(n, 0));
for ( int i = n - 1; i >= 0; --i) {
dp[i][i] = 1;
for ( int j = i + 1; j < n; ++j) {
dp[i][j] = INT_MAX;
for ( int k = i; k < j; ++k) {
int minOperations = dp[i][k] + dp[k + 1][j];
if (s[i] == s[j])
minOperations--;
dp[i][j] = min(dp[i][j], minOperations);
}
}
}
return dp[0][n - 1];
}
int main()
{
string s = "aabca" ;
cout << writeString(s);
return 0;
}
|
Java
public class WriteString {
public static int writeString(String s)
{
int n = s.length();
int [][] dp = new int [n][n];
for ( int i = n - 1 ; i >= 0 ; i--) {
dp[i][i] = 1 ;
for ( int j = i + 1 ; j < n; j++) {
dp[i][j] = Integer.MAX_VALUE;
for ( int k = i; k < j; k++) {
int minOperations
= dp[i][k] + dp[k + 1 ][j];
if (s.charAt(i) == s.charAt(j))
minOperations--;
dp[i][j]
= Math.min(dp[i][j], minOperations);
}
}
}
return dp[ 0 ][n - 1 ];
}
public static void main(String[] args)
{
String s = "aabca" ;
System.out.println(writeString(s));
}
}
|
Python3
def writeString(s):
n = len (s)
dp = [[ 0 ] * n for _ in range (n)]
for i in range (n - 1 , - 1 , - 1 ):
dp[i][i] = 1
for j in range (i + 1 , n):
dp[i][j] = float ( 'inf' )
for k in range (i, j):
minOperations = dp[i][k] + dp[k + 1 ][j]
if s[i] = = s[j]:
minOperations - = 1
dp[i][j] = min (dp[i][j], minOperations)
return dp[ 0 ][n - 1 ]
s = "aabca"
print (writeString(s))
|
C#
using System;
class MainClass
{
static int WriteString( string s)
{
int n = s.Length;
int [,] dp = new int [n, n];
for ( int i = n - 1; i >= 0; i--)
{
dp[i, i] = 1;
for ( int j = i + 1; j < n; j++)
{
dp[i, j] = int .MaxValue;
for ( int k = i; k < j; k++)
{
int minOperations = dp[i, k] + dp[k + 1, j];
if (s[i] == s[j])
minOperations--;
dp[i, j] = Math.Min(dp[i, j], minOperations);
}
}
}
return dp[0, n - 1];
}
public static void Main( string [] args)
{
string s = "aabca" ;
Console.WriteLine(WriteString(s));
}
}
|
Javascript
function writeString(s) {
const n = s.length;
const dp = new Array(n).fill(0).map(() => new Array(n).fill(0));
for (let i = n - 1; i >= 0; --i) {
dp[i][i] = 1;
for (let j = i + 1; j < n; ++j) {
dp[i][j] = Infinity;
for (let k = i; k < j; ++k) {
let minOperations = dp[i][k] + dp[k + 1][j];
if (s[i] === s[j]) {
minOperations--;
}
dp[i][j] = Math.min(dp[i][j], minOperations);
}
}
}
return dp[0][n - 1];
}
const s = "aabca" ;
console.log(writeString(s));
|
Time Complexity: O(n^3), where n is length of given string.
Auxiliary Space: O(n*n)
Share your thoughts in the comments
Please Login to comment...