Given a string str consisting of lowercase English alphabets and an array of positive integer arr[] both of same length. The task is to remove some characters from the given string such that no sub-sequence in 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 character 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 |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum cost to make Longest Common Subsequence of length k
- Count of operations to make a binary string"ab" free
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Minimum Cost To Make Two Strings Identical
- Minimum cost to make two strings identical by deleting the digits
- Minimum Cost to make two Numeric Strings Identical
- Minimum cost to make two strings same
- Minimum cost required to rearrange a given array to make it equal to another given array
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Cost to make a string Panagram
- Cost to make a string Panagram | Set 2
- Cost of creating smallest subsequence with sum of difference between adjacent elements maximum
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Find the equal pairs of subsequence of S and subsequence of T
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Minimum cost to convert string into palindrome
- Minimum cost to modify a string
- Minimum cost to partition the given binary string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

