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)
- Update DPi = DPi-1 – 1
- else update DPi = DPi-1 + 2
-
If (Si > Si-1)
- Return DPN-1.
Code to implement the approach:
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std;
// Function to Minimum operations to make string xyz type int minimumOperations(string S, int N)
{ // DP array initalized with 0
vector< int > DP(N, 0);
// base case
DP[0] = 2;
// Calculating answer for i'th state
for ( int i = 1; i < N; i++) {
if (S[i] > S[i - 1])
// last characters can be of type
// xy, yz, xz
DP[i] = DP[i - 1] - 1;
else
// either x, y or z
DP[i] = DP[i - 1] + 2;
}
// Returing answer
return DP[N - 1];
} // Driver Function int main()
{ // Input
int N = 2;
string S = "XY" ;
// Function Call
cout << minimumOperations(S, N) << endl;
return 0;
} |
// Java program for the above approach import java.util.*;
public class GFG {
// Function to Minimum operations to make string xyz
// type
static int minimumOperations(String S, int N)
{
// DP array initialized with 0
int [] DP = new int [N];
// base case
DP[ 0 ] = 2 ;
// Calculating answer for i'th state
for ( int i = 1 ; i < N; i++) {
if (S.charAt(i) > S.charAt(i - 1 ))
// last characters can be of type
// xy, yz, xz
DP[i] = DP[i - 1 ] - 1 ;
else
// either x, y or z
DP[i] = DP[i - 1 ] + 2 ;
}
// Returning answer
return DP[N - 1 ];
}
// Driver Function
public static void main(String[] args)
{
// Input
int N = 2 ;
String S = "XY" ;
// Function Call
System.out.println(minimumOperations(S, N));
}
} // This code is contributed by Susobhan Akhuli |
using System;
public class GFG {
// Function to find the minimum operations to make
// string xyz type
static int MinimumOperations( string S, int N)
{
// DP array initialized with 0
int [] DP = new int [N];
// Base case
DP[0] = 2;
// Calculating answer for i'th state
for ( int i = 1; i < N; i++) {
if (S[i] > S[i - 1])
// Last characters can be of type xy, yz, xz
DP[i] = DP[i - 1] - 1;
else
// Either x, y or z
DP[i] = DP[i - 1] + 2;
}
// Returning answer
return DP[N - 1];
}
// Driver Function
public static void Main( string [] args)
{
// Input
int N = 2;
string S = "XY" ;
// Function Call
Console.WriteLine(MinimumOperations(S, N));
}
} // This code is contributed by Susobhan Akhuli |
// Function to Minimum operations to make string xyz type function minimumOperations(S, N) {
// DP array initialized with 0
let DP = new Array(N).fill(0);
// base case
DP[0] = 2;
// Calculating answer for i'th state
for (let i = 1; i < N; i++) {
if (S[i] > S[i - 1])
// last characters can be of type
// xy, yz, xz
DP[i] = DP[i - 1] - 1;
else
// either x, y or z
DP[i] = DP[i - 1] + 2;
}
// Returning answer
return DP[N - 1];
} // Driver Function function main() {
// Input
let N = 2;
let S = "XY" ;
// Function Call
console.log(minimumOperations(S, N));
} // Calling the main function main(); // This code is contributed by shivamgupta310570 |
# Python code to implement the approach # Function to Minimum operations to make string xyz type def minimum_operations(S, N):
# DP array initialized with 0
DP = [ 0 ] * N
# base case
DP[ 0 ] = 2
# Calculating answer for i'th state
for i in range ( 1 , N):
if S[i] > S[i - 1 ]:
# last characters can be of type xy, yz, xz
DP[i] = DP[i - 1 ] - 1
else :
# either x, y or z
DP[i] = DP[i - 1 ] + 2
# Returning answer
return DP[N - 1 ]
# Driver Function def main():
# Input
N = 2
S = "XY"
# Function Call
print (minimum_operations(S, N))
if __name__ = = "__main__" :
main()
|
1
Time Complexity: O(N)
Auxiliary Space: O(N)