Techno Blender
Digitally Yours.

Generate longest String with character sum at most K by deleting letters

0 34


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given a string str and an integer K, the task is to find the longest string that can be made by deleting letters from the given string such that the sum of the characters of the remaining string is at most K.

Note: The value of the string is calculated as the sum of the value of characters (a value is 1, b value is 2…., z value is 26).

Examples:

Input:  str = “geeksforgeeks”, K = 15
Output: eee
Explanation: After deleting the characters string gets reduced to eee whose value is 5 + 5 +  5 = 15 which is less than or equal to K i.e. 15

Input: str = “abca”, K = 6
Output: aba
Explanation: Initial value of str 1 + 2 + 3 + 1 = 7, after deleting the letter c, the string gets reduced to aba whose value is 1+2+1 = 4 which is less than K i.e. 6.

Approach: The problem can be solved using Greedy approach based on the following idea:

We must delete letters with the highest value first. So, we sort the string str in decreasing order and will be deleting letters from the starting of string str as long as the initial value of string str is greater than the given value K.

Follow the steps mentioned below to implement the idea:

  • Calculate the initial value of the given string.
  • Sort the string str in decreasing order
  • Start removing the value of the current letter from the initial value as long as the initial value is greater than K.
    • Store the removed characters in the map
  • Iterate through the given string once again and 
    • If the current letter does not exist in the map take it into the resultant string
    • Else decrease the frequency of the letter
  • Return the resultant string

Below is the implementation of the above approach:

C++

  

#include <bits/stdc++.h>

using namespace std;

  

string minimise_str(string str, int K)

{

    int initial_value = 0;

  

    

    for (int i = 0; i < str.length(); i++) {

        initial_value += (str[i] - 'a') + 1;

    }

  

    string temp = str;

  

    

    sort(str.begin(), str.end());

    reverse(str.begin(), str.end());

  

    

    unordered_map<char, int> mpp;

    int i = 0;

  

    

    

    while (initial_value > K) {

        initial_value -= (str[i] - 'a') + 1;

        mpp[str[i]]++;

        i++;

    }

  

    

    string ans = "";

    for (int i = 0; i < temp.size(); i++) {

  

        

        

        if (mpp[temp[i]] > 0) {

            mpp[temp[i]]--;

        }

        

        

        else {

            ans += temp[i];

        }

    }

  

    

    return ans;

}

  

int main()

{

    string str = "geeksforgeeks";

    int K = 15;

  

    

    cout << minimise_str(str, K);

  

    return 0;

}

Time Complexity: O(N * logN)
Auxiliary Space: O(N)

Related Articles:


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given a string str and an integer K, the task is to find the longest string that can be made by deleting letters from the given string such that the sum of the characters of the remaining string is at most K.

Note: The value of the string is calculated as the sum of the value of characters (a value is 1, b value is 2…., z value is 26).

Examples:

Input:  str = “geeksforgeeks”, K = 15
Output: eee
Explanation: After deleting the characters string gets reduced to eee whose value is 5 + 5 +  5 = 15 which is less than or equal to K i.e. 15

Input: str = “abca”, K = 6
Output: aba
Explanation: Initial value of str 1 + 2 + 3 + 1 = 7, after deleting the letter c, the string gets reduced to aba whose value is 1+2+1 = 4 which is less than K i.e. 6.

Approach: The problem can be solved using Greedy approach based on the following idea:

We must delete letters with the highest value first. So, we sort the string str in decreasing order and will be deleting letters from the starting of string str as long as the initial value of string str is greater than the given value K.

Follow the steps mentioned below to implement the idea:

  • Calculate the initial value of the given string.
  • Sort the string str in decreasing order
  • Start removing the value of the current letter from the initial value as long as the initial value is greater than K.
    • Store the removed characters in the map
  • Iterate through the given string once again and 
    • If the current letter does not exist in the map take it into the resultant string
    • Else decrease the frequency of the letter
  • Return the resultant string

Below is the implementation of the above approach:

C++

  

#include <bits/stdc++.h>

using namespace std;

  

string minimise_str(string str, int K)

{

    int initial_value = 0;

  

    

    for (int i = 0; i < str.length(); i++) {

        initial_value += (str[i] - 'a') + 1;

    }

  

    string temp = str;

  

    

    sort(str.begin(), str.end());

    reverse(str.begin(), str.end());

  

    

    unordered_map<char, int> mpp;

    int i = 0;

  

    

    

    while (initial_value > K) {

        initial_value -= (str[i] - 'a') + 1;

        mpp[str[i]]++;

        i++;

    }

  

    

    string ans = "";

    for (int i = 0; i < temp.size(); i++) {

  

        

        

        if (mpp[temp[i]] > 0) {

            mpp[temp[i]]--;

        }

        

        

        else {

            ans += temp[i];

        }

    }

  

    

    return ans;

}

  

int main()

{

    string str = "geeksforgeeks";

    int K = 15;

  

    

    cout << minimise_str(str, K);

  

    return 0;

}

Time Complexity: O(N * logN)
Auxiliary Space: O(N)

Related Articles:

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment