Minimum cost to make a string free of a subsequence
Last Updated :
15 Dec, 2022
Given string str consisting of lowercase English alphabets and an array of positive integer arr[] both of the same length. The task is to remove some characters from the given string such that no sub-sequence in the string forms the string "code". Cost of removing a character str[i] is arr[i]. Find the minimum cost to achieve the target.
Examples:
Input: str = "code", arr[] = {3, 2, 1, 3}
Output: 1
Remove 'd' which costs the minimum.
Input: str = "ccooddde", arr[] = {3, 2, 1, 3, 3, 5, 1, 6}
Output: 4
Remove both the 'o' which cost 1 + 3 = 4
Approach: If any sub-sequence with "code" is possible, then the removal of a single character is required. Cost for the removal of each character is given in arr[]. So, traverse the string and for each character which is either c, o, d or e calculate the cost of their removal. And finally, the minimum among the cost of removal of all characters is required minimum cost.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the minimum cost
int findCost(string str, int arr[], int n)
{
long long costofC = 0, costofO = 0,
costofD = 0, costofE = 0;
// Traverse the string
for (int i = 0; i < n; i++) {
// Min Cost to remove 'c'
if (str[i] == 'c')
costofC += arr[i];
// Min Cost to remove subsequence "co"
else if (str[i] == 'o')
costofO = min(costofC, costofO + arr[i]);
// Min Cost to remove subsequence "cod"
else if (str[i] == 'd')
costofD = min(costofO, costofD + arr[i]);
// Min Cost to remove subsequence "code"
else if (str[i] == 'e')
costofE = min(costofD, costofE + arr[i]);
}
// Return the minimum cost
return costofE;
}
// Driver program
int main()
{
string str = "geekcodergeeks";
int arr[] = { 1, 2, 1, 3, 4, 2, 6, 4, 6, 2, 3, 3, 3, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << findCost(str, arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the minimum cost
static int findCost(String str, int arr[], int n)
{
long costofC = 0, costofO = 0,
costofD = 0, costofE = 0;
// Traverse the string
for (int i = 0; i < n; i++) {
// Min Cost to remove 'c'
if (str.charAt(i) == 'c')
costofC += arr[i];
// Min Cost to remove subsequence "co"
else if (str.charAt(i) == 'o')
costofO = Math.min(costofC, costofO + arr[i]);
// Min Cost to remove subsequence "cod"
else if (str.charAt(i) == 'd')
costofD = Math.min(costofO, costofD + arr[i]);
// Min Cost to remove subsequence "code"
else if (str.charAt(i) == 'e')
costofE = Math.min(costofD, costofE + arr[i]);
}
// Return the minimum cost
return (int)costofE;
}
// Driver program
public static void main(String[] args)
{
String str = "geekcodergeeks";
int arr[] = { 1, 2, 1, 3, 4, 2, 6, 4, 6, 2, 3, 3, 3, 2 };
int n = arr.length;
System.out.print(findCost(str, arr, n));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to return the minimum cost
def findCost(str, arr, n):
costofC, costofO = 0, 0
costofD, costofE = 0, 0
# Traverse the string
for i in range(n):
# Min Cost to remove 'c'
if (str[i] == 'c'):
costofC += arr[i]
# Min Cost to remove subsequence "co"
elif (str[i] == 'o'):
costofO = min(costofC, costofO + arr[i])
# Min Cost to remove subsequence "cod"
elif (str[i] == 'd'):
costofD = min(costofO, costofD + arr[i])
# Min Cost to remove subsequence "code"
elif (str[i] == 'e'):
costofE = min(costofD, costofE + arr[i])
# Return the minimum cost
return costofE
# Driver Code
if __name__ == '__main__':
str = "geekcodergeeks"
arr = [1, 2, 1, 3, 4, 2, 6, 4, 6, 2, 3, 3, 3, 2]
n = len(arr)
print(findCost(str, arr, n))
# This code contributed by PrinciRaj1992
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the minimum cost
public static int findCost(string str,
int[] arr, int n)
{
long costofC = 0, costofO = 0,
costofD = 0, costofE = 0;
// Traverse the string
for (int i = 0; i < n; i++)
{
// Min Cost to remove 'c'
if (str[i] == 'c')
{
costofC += arr[i];
}
// Min Cost to remove subsequence "co"
else if (str[i] == 'o')
{
costofO = Math.Min(costofC, costofO + arr[i]);
}
// Min Cost to remove subsequence "cod"
else if (str[i] == 'd')
{
costofD = Math.Min(costofO, costofD + arr[i]);
}
// Min Cost to remove subsequence "code"
else if (str[i] == 'e')
{
costofE = Math.Min(costofD, costofE + arr[i]);
}
}
// Return the minimum cost
return (int)costofE;
}
// Driver program
public static void Main(string[] args)
{
string str = "geekcodergeeks";
int[] arr = new int[] {1, 2, 1, 3, 4, 2, 6,
4, 6, 2, 3, 3, 3, 2};
int n = arr.Length;
Console.Write(findCost(str, arr, n));
}
}
// This code is contributed by shrikanth13
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the Math.minimum cost
function findCost(str, arr, n)
{
var costofC = 0, costofO = 0,
costofD = 0, costofE = 0;
// Traverse the string
for (var i = 0; i < n; i++) {
// Math.min Cost to remove 'c'
if (str[i] == 'c')
costofC += arr[i];
// Math.min Cost to remove subsequence "co"
else if (str[i] == 'o')
costofO = Math.min(costofC, costofO + arr[i]);
// Math.min Cost to remove subsequence "cod"
else if (str[i] == 'd')
costofD = Math.min(costofO, costofD + arr[i]);
// Math.min Cost to remove subsequence "code"
else if (str[i] == 'e')
costofE = Math.min(costofD, costofE + arr[i]);
}
// Return the Math.minimum cost
return costofE;
}
// Driver program
var str = "geekcodergeeks";
var arr = [ 1, 2, 1, 3, 4, 2, 6, 4, 6, 2, 3, 3, 3, 2 ];
var n = arr.length;
document.write( findCost(str, arr, n));
</script>
Time Complexity: O(n), where n is the size of the given array and string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
Minimum cost to make Longest Common Subsequence of length k Given two string X, Y and an integer k. Now the task is to convert string X with the minimum cost such that the Longest Common Subsequence of X and Y after conversion is of length k. The cost of conversion is calculated as XOR of old character value and new character value. The character value of 'a
14 min read
Minimum deletions to make String S the Subsequence of any String in the Set D Given a string S and a set of strings D, the task is to find the minimum number of deletions required to make the string S a subsequence of any string in the set D, such that the deletion can only be performed on the substring of S. Examples: Input: string S = "abcdefg", vector<string> D = {"a
13 min read
Minimum cost for constructing the subsequence of length K from given string S Given a string S consisting of N lowercase English alphabets, and an integer K and, an array cost[] of size 26 denoting the cost of each lowercase English alphabet, the task is to find the minimum cost to construct a subsequence of length K from the characters of the string S. Examples: Input: S = "
11 min read
Minimize total cost of picking K unique subsequences from given string Given a string S of length N and a positive integer K, the task is to find the minimum total cost of picking K unique subsequence of the given string S such that the cost of picking a subsequence is the (length of S - length of that subsequence). If it is impossible to choose K unique subsequence, t
8 min read
Print all subsequences of a string Given a string, we have to find out all its subsequences of it. A String is said to be a subsequence of another String, if it can be obtained by deleting 0 or more character without changing its order.Examples: Input : abOutput : "", "a", "b", "ab"Input : abcOutput : "", "a", "b", "c", "ab", "ac", "
12 min read
Minimum cost to delete characters from String A to remove any subsequence as String B Given two strings A and B of size N and M respectively, where B is a sub-sequence of A and an array arr[] of size N, where arr[i] is the cost to delete ith character from string A. The task is to find the minimum cost to delete characters from A such that after deletion no subsequence of A is the sa
15+ min read