Minimum cost for constructing the subsequence of length K from given string S


We will be given a string of length n, an integer k, and an integer array of length 26. The integer array defines the cost of each lowercase character and the string will contain only lowercase letters. We have to create a subsequence of length k from the given string at the minimum possible cost. We will use sorting to solve the problem and will implement a code with a full explanation.

Sample Examples

Input 1

Given string: acbcbac

Given number: 4

Given array: {2,3,1,2,4,5,5,6,6,2,1,0,4,3,5,2,3,6,2,6,0,9,3,1,5,6}

Output 1: 6 (acca)

Explanation − The output of the above code is based on the cost of the elements or the characters. We have three different characters a,b, and c. cost of the c is minimum, and a is better in cost as compared to b.

Input 2

Given string: zxay

Given number: 2

Given array: {2,3,1,2,4,5,5,6,6,2,1,0,4,3,5,2,3,6,2,6,0,9,3,1,5,6}

Output 2: 3 (xa)

Explanation − Again, the same reason or explanation, here the cost of x and a is minimum as compared to both of them.

Sorting Approach

In this approach, we are going to create an extra string which will be a copy of the given string.

We will sort the temporary string using the sort function and the inline lambda function.

Lambda functions are the inline function that can be inserted in the STL function and used to change the way of the sorting priority.

After sorting we will traverse over the string for the first k characters only and will add their cost to the answer variable and will return that.

Example

#include <bits/stdc++.h>
using namespace std;
// function to find the required string 
int getString(string str, int k, int cost[]){
   // creating a temporary string 
   string temp = str;    
   // sorting the temporary string on the basis of the priority of the elements 
   sort(temp.begin(),temp.end(),[&](char a, char b){
      return cost[a-'a'] < cost[b-'a']; 
   });    
   int ans = 0; // variable to store the answer     
   // getting cost of the first k elements     
   for(int i=0; i<k; i++){
      ans += cost[temp[i]-'a'];
   }
   return ans;
}
int main(){
   string str = "acbacaz"; // given string 
   int k = 5; // size of subsequence    
   // cost array
   int cost[] = {2,3,1,2,4,5,5,6,6,2,1,0,4,3,5,2,3,6,2,6,0,9,3,1,5,6};
   // calling the function to get the string 
   int req = getString(str,k,cost);
   cout<<"Cost for the subsequence with the lowest cost of size "<< k << " is "<<req<<endl;
   return 0;
}

Output

Cost for the subsequence with the lowest cost of size 5 is 8

Time and Space Complexity

The time complexity of the above code is O(N*log(N)) where N is the size of the array. We were sorting the array bringing the logarithmic factor.

The space complexity of the above code is O(N), which is due to creating the temporary string.

Multiset Approach

In this approach, we will traverse over the string and add the elements to the set.

If the size of the set is less than the k then we can add that element directly to set and add the cost in the answer variable.

If the size of the set is equal to k, then we will compare the cost of the current character to the maximum cost present in the multiset.

If the cost is low, then we will remove the last element and reduce that cost and add the new cost to the multiset as well as the answer.

At last, we will return the cost. We can also use the priority queue data structure instead of multiset.

Example

#include <bits/stdc++.h>
using namespace std;
// function to find the required string 
int getString(string str, int k, int cost[]){    
   // iterating over the string and adding the cost to set 
   multiset<int>st;
   int ans = 0;
   for(int i =0; i< str.length() ;i++){
      if(st.size() < k){
         ans += cost[str[i]-'a'];
         st.insert(cost[str[i]-'a']);
      }
      else{
         if(*st.rbegin() > cost[str[i]-'a']){
            ans -= *st.rbegin()-cost[str[i]-'a'];
            st.insert(cost[str[i]-'a']);
            st.erase(*st.rbegin());
         }
      }
   }
   return ans;
}
int main(){
   string str = "acbacaz"; // given string 
   int k = 5; // size of subsequence    
   // cost array
   int cost[] = {2,3,1,2,4,5,5,6,6,2,1,0,4,3,5,2,3,6,2,6,0,9,3,1,5,6};
   // calling the function to get the string 
   int req = getString(str,k,cost);    
   cout<<"The cost for the subsequence with the lowest cost of size "<< k << " is "<<req<<endl;
   return 0;
}

Output

The cost for the subsequence with the lowest cost of size 5 is 8

Time and Space Complexity

The time complexity of the above code is O(N*log(K)), where N is the size of the string and k is the size of the required subsequence.

The space complexity of the above code is O(K), because the maximum size of the set will be K.

Note: We can use the priority queue data structure also here to find the minimum cost and that method will bring more clean code. Because, accessing the greatest cost is much easier with the priority queue.

Conclusion

In this tutorial we have implemented a program to find the minimum cost required to create a subsequence from the given string where cost of each character is given. We have implemented a multiset method and another sorting method. For the sorting method, we have used the lambda function which will do the custom sorting.

Updated on: 17-May-2023

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements