Form N-copy string with add, remove and append operations
Last Updated :
13 Sep, 2022
A string is called a N-copy string when it is formed by N copies of letter 'G'. ie "GGGG" is a 4-copy string since it contains 4 copies of the letter 'G'. Initially we have a empty string on which we can perform the following three operations:
- Add a single letter 'G' with a cost of X.
- Remove a single letter 'G' with a cost of X.
- Append the string formed till now to itself with a cost of Y, i.e we have the string 'GG' we can append it to itself to make 'GGGG'
Given N, X and Y find the minimum cost to generate the N-copy string using the above operations
Examples:
Input : N = 4, X = 2, Y = 1
Output : 4
Explanation:
- Initially, the string it empty, we perform one Add operation to form string 'G'.
- Next, we perform a operation of type 3, and form the string 'GG'.
- Again we perform the operation 3 and form 'GGGG', which is the required N-copy string.
Cost = X + Y + Y = 2 + 1 + 1 = 4
Input : N = 4, X = 1, Y = 3
Output : 4
We can perform 4 consecutive Add operations to form 'GGGG'.
Cost = X + X + X + X = 1 + 1 + 1 + 1 = 4
The Problem can be solved using a Dynamic Programming Approach.
If we carefully analyse the operations, it is easy to see that we perform the remove operation only when there is an extra copy than what is required, this can only be the case when before this remove operation there was a operation of type 3 involved, because if there was an operation of type 1 before it, then it is meaningless as consecutive add and remove operations only increase the cost, without introducing any new copies into our string.
Thus we can say we have the following operations,
- Add a single character 'G', with cost X
- Append the string to itself and then remove a single character 'G', with cost Y + X
- Append the string to itself, with cost Y
Note: Now on we refer to these modified operations
Now, it can be solved with a O(n) DP approach.
Let dp[i] represent the minimum cost to form a i-copy string
then the state transitions can be:
- If i is odd,
- Case 1: Add a character to the (i-1)-copy string,
- Case 2: Perform an operation of type 2 on the ((i+1)/2)-copy string
- i.e dp[i] = min(dp[i - 1] + X, dp[(i + 1) / 2)] + Y + X)
- If i is even
- Case 1: Add a character to the (i-1)-copy string,
- Case 2: Perform an operation of type 3 on the (i/2)-copy string
- i.e dp[i] = min(dp[i - 1] + X, dp[i / 2] + Y)
Implementation:
C++
// CPP code to find minimum cost to form a
// N-copy string
#include <bits/stdc++.h>
using namespace std;
// Returns the minimum cost to form a n-copy string
// Here, x -> Cost to add/remove a single character 'G'
// and y-> cost to append the string to itself
int findMinimumCost(int n, int x, int y)
{
int* dp = new int[n + 1];
// Base Case: to form a 1-copy string we
// need to perform an operation of type
// 1(i.e Add)
dp[1] = x;
for (int i = 2; i <= n; i++) {
if (i & 1) {
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 2 operation
// on ((i + 1) / 2)-copy string
dp[i] = min(dp[i - 1] + x, dp[(i + 1) / 2] + y + x);
}
else {
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 3 operation on
// (i/2)-copy string
dp[i] = min(dp[i - 1] + x, dp[i / 2] + y);
}
}
return dp[n];
}
// Driver Code
int main()
{
int n = 4, x = 2, y = 1;
cout << findMinimumCost(n, x, y);
return 0;
}
Java
// Java code to find minimum cost to form a
// N-copy string
class Solution
{
// Returns the minimum cost to form a n-copy string
// Here, x -> Cost to add/remove a single character 'G'
// and y-> cost to append the string to itself
static int findMinimumCost(int n, int x, int y)
{
int dp[] = new int[n + 1];
// Base Case: to form a 1-copy string we
// need to perform an operation of type
// 1(i.e Add)
dp[1] = x;
for (int i = 2; i <= n; i++) {
if ((i & 1)!=0) {
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 2 operation
// on ((i + 1) / 2)-copy string
dp[i] = Math.min(dp[i - 1] + x, dp[(i + 1) / 2] + y + x);
}
else {
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 3 operation on
// (i/2)-copy string
dp[i] = Math.min(dp[i - 1] + x, dp[i / 2] + y);
}
}
return dp[n];
}
// Driver Code
public static void main(String args[])
{
int n = 4, x = 2, y = 1;
System.out.println( findMinimumCost(n, x, y));
}
}
//contributed by Arnab Kundu
Python3
# Python3 code to find minimum cost to
# form a N-copy string
# function returns the minimum cost to
# form a n-copy string Here, x->Cost to
# add/remove a single character 'G' and
# y->cost to append the string to itself
def findMinimumCost(n, x, y):
dp = [0 for i in range(n + 1)]
# base case: ro form a 1-copy string
# we need tp perform an operation
# of type 1(i,e Add)
dp[1] = x
for i in range(2, n + 1):
if i & 1:
# case1. Perform a Add operation
# on (i-1)copy string
# case2. perform a type 2 operation
# on((i+1)/2)-copy string
dp[i] = min(dp[i - 1] + x,
dp[(i + 1) // 2] + y + x)
else:
# case1. Perform a Add operation
# on (i-1)-copy string
# case2. Perform a type # operation
# on (i/2)-copy string
dp[i] = min(dp[i - 1] + x,
dp[i // 2] + y)
return dp[n]
# Driver code
n, x, y = 4, 2, 1
print(findMinimumCost(n, x, y))
# This code is contributed
# by Mohit Kumar
C#
// C# code to find minimum cost to form a
// N-copy string
using System;
class GFG
{
// Returns the minimum cost to form a n-copy string
// Here, x -> Cost to add/remove a single character 'G'
// and y-> cost to append the string to itself
static int findMinimumCost(int n, int x, int y)
{
int[] dp = new int[n + 1];
// Base Case: to form a 1-copy string we
// need to perform an operation of type
// 1(i.e Add)
dp[1] = x;
for (int i = 2; i <= n; i++)
{
if ((i & 1)!=0)
{
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 2 operation
// on ((i + 1) / 2)-copy string
dp[i] = Math.Min(dp[i - 1] + x,
dp[(i + 1) / 2] + y + x);
}
else
{
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 3 operation on
// (i/2)-copy string
dp[i] = Math.Min(dp[i - 1] + x,
dp[i / 2] + y);
}
}
return dp[n];
}
// Driver Code
public static void Main()
{
int n = 4, x = 2, y = 1;
Console.WriteLine(findMinimumCost(n, x, y));
}
}
// This code is contributed
// by Akanksha Rai
PHP
<?php
// PHP code to find minimum cost to form a
// N-copy string
// Returns the minimum cost to form a n-copy
// string. Here, x -> Cost to add/remove a
// single character 'G' and y-> cost to
// append the string to itself
function findMinimumCost($n, $x, $y)
{
$dp[$n + 1] = array();
// Base Case: to form a 1-copy string
// we need to perform an operation of
// type 1(i.e Add)
$dp[1] = $x;
for ($i = 2; $i <= $n; $i++)
{
if ($i & 1)
{
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 2 operation
// on ((i + 1) / 2)-copy string
$dp[$i] = min($dp[$i - 1] + $x,
$dp[($i + 1) / 2] + $y + $x);
}
else
{
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 3 operation on
// (i/2)-copy string
$dp[$i] = min($dp[$i - 1] + $x,
$dp[$i / 2] + $y);
}
}
return $dp[$n];
}
// Driver Code
$n = 4;
$x = 2;
$y = 1;
echo findMinimumCost($n, $x, $y);
// This code is contributed by Sach_Code
?>
JavaScript
<script>
// Javascript code to find minimum cost to form a
// N-copy string
// Returns the minimum cost to form a n-copy string
// Here, x -> Cost to add/remove a single character 'G'
// and y-> cost to append the string to itself
function findMinimumCost(n, x, y)
{
let dp = new Array(n + 1);
// Base Case: to form a 1-copy string we
// need to perform an operation of type
// 1(i.e Add)
dp[1] = x;
for (let i = 2; i <= n; i++) {
if ((i & 1)!=0) {
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 2 operation
// on ((i + 1) / 2)-copy string
dp[i] = Math.min(dp[i - 1] + x, dp[parseInt((i + 1) / 2, 10)] + y + x);
}
else {
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 3 operation on
// (i/2)-copy string
dp[i] = Math.min(dp[i - 1] + x, dp[parseInt(i / 2, 10)] + y);
}
}
return dp[n];
}
let n = 4, x = 2, y = 1;
document.write(findMinimumCost(n, x, y));
</script>
Complexity Analysis:
- Time Complexity O(n)
- Auxiliary Space O(n)
Similar Reads
Converting one string to other using append and delete last operations Given an integer k and two strings str1 and str2 determine whether or not we can convert str1 to str2 by performing exactly k of the below operations on str1. Append a lowercase English alphabetic letter to the end of the str1. Delete the last character in str1 (Performing this operation on an empty
7 min read
Function to copy string (Iterative and Recursive) Given two strings, copy one string to another using recursion. We basically need to write our own recursive version of strcpy in C/C++ Examples:Â Input : s1 = "hello" s2 = "geeksforgeeks" Output : s2 = "hello" Input : s1 = "geeksforgeeks" s2 = "" Output : s2 = "geeksforgeeks" Iterative:Â Copy every c
6 min read
Find the minimum operations required to type the given String Geek is extremely punctual but today even he is not feeling like doing his homework assignment. He must start doing it immediately in order to meet the deadline. For the assignment, Geek needs to type a string s.To reduce his workload, he has decided to perform one of the following two operations ti
4 min read
std::string::remove_copy(), std::string::remove_copy_if() in C++ remove_copy() It is an STL function in c++ which is defined in algorithm library. It copies the elements in the range [first, last) to the range beginning at result, except those elements that compare equal to given elements. The resulting range is shorter than [first,last) by as many elements as ma
5 min read
Minimal moves to form a string by adding characters or appending string itself Given a string S, we need to write a program to check if it is possible to construct the given string S by performing any of the below operations any number of times. In each step, we can: Add any character at the end of the string.or, append the string to the string itself.The above steps can be ap
8 min read
Find lexicographical smallest string by performing the given operations N times Given a string S of N characters, the task is to find the smallest lexicographical string after performing each of the following operations N times in any order: Remove the 1st character of S and insert it into a stack X.Remove the top of stack X and append it to the end of another string Y which is
8 min read