Minimum insertion to make consecutive ‘XYZ’ substrings
Last Updated :
04 Mar, 2024
Given string S consisting of characters ‘X’, ‘Y’ and ‘Z’ only. The your task is to find minimum number of operations required to make string containing only consecutive “XYZ” substrings given that you are allowed to choose any one out of three characters and insert it anywhere in S.
Examples:
Input: S = XXX
Output: 6
Explanation: Insert 3 Y and Z at optimal positions. So that S becomes = XYZXYZXYZ. Now, S contains only consecutive XYZ substrings. So, in total we insert 6 characters (3 times Y + 3 times Z) in 6 operations. Therefore, output is 6.
Input: S = Y
Output: 2
Explanation: It can be verified that only 2 operations will be required to satisfying problem constraint.
Approach: Implement the idea below to solve the problem
As we have choice to insert character at any position. Then, Dynamic Programming can be used to solve this problem. The main concept of DP in the problem will be:
DP[X] will store the minimum operations to make first X characters of string S containing only substring XYZ.
Transition:
- DPi = DPi + DPi -1 – 1 (if Si > Si-1)
- DPi = DPi + DPi-1 + 2 (Otherwise)
Steps were taken to solve the problem:
- Declare an array let say DP[] of size N.
- Set base case as DP0 = 2.
- Iterating from i = 1 to N – 1 and follow below-mentioned steps:
- If (Si > Si-1)
- else update DPi = DPi-1 + 2
- Return DPN-1.
Code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumOperations(string S, int N)
{
vector< int > DP(N, 0);
DP[0] = 2;
for ( int i = 1; i < N; i++) {
if (S[i] > S[i - 1])
DP[i] = DP[i - 1] - 1;
else
DP[i] = DP[i - 1] + 2;
}
return DP[N - 1];
}
int main()
{
int N = 2;
string S = "XY" ;
cout << minimumOperations(S, N) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int minimumOperations(String S, int N)
{
int [] DP = new int [N];
DP[ 0 ] = 2 ;
for ( int i = 1 ; i < N; i++) {
if (S.charAt(i) > S.charAt(i - 1 ))
DP[i] = DP[i - 1 ] - 1 ;
else
DP[i] = DP[i - 1 ] + 2 ;
}
return DP[N - 1 ];
}
public static void main(String[] args)
{
int N = 2 ;
String S = "XY" ;
System.out.println(minimumOperations(S, N));
}
}
|
C#
using System;
public class GFG {
static int MinimumOperations( string S, int N)
{
int [] DP = new int [N];
DP[0] = 2;
for ( int i = 1; i < N; i++) {
if (S[i] > S[i - 1])
DP[i] = DP[i - 1] - 1;
else
DP[i] = DP[i - 1] + 2;
}
return DP[N - 1];
}
public static void Main( string [] args)
{
int N = 2;
string S = "XY" ;
Console.WriteLine(MinimumOperations(S, N));
}
}
|
Javascript
function minimumOperations(S, N) {
let DP = new Array(N).fill(0);
DP[0] = 2;
for (let i = 1; i < N; i++) {
if (S[i] > S[i - 1])
DP[i] = DP[i - 1] - 1;
else
DP[i] = DP[i - 1] + 2;
}
return DP[N - 1];
}
function main() {
let N = 2;
let S = "XY" ;
console.log(minimumOperations(S, N));
}
main();
|
Python3
def minimum_operations(S, N):
DP = [ 0 ] * N
DP[ 0 ] = 2
for i in range ( 1 , N):
if S[i] > S[i - 1 ]:
DP[i] = DP[i - 1 ] - 1
else :
DP[i] = DP[i - 1 ] + 2
return DP[N - 1 ]
def main():
N = 2
S = "XY"
print (minimum_operations(S, N))
if __name__ = = "__main__" :
main()
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...