Techno Blender
Digitally Yours.

Longest Subsequence with difference between characters equals to K

0 13


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given a string S consisting of lowercase letters. Find the longest subsequence of S such that the difference between the maximum and minimum occurring characters in the subsequence is exactly K.

Examples:

Input: S = ‘abcdeg’ and K = 4
Output: abcde

Input: S = ‘daaaabbbadddddeeee’, K = 1
Output: ddddddeeee

Approach: This can be solved with the following idea:

Iterate through all possible minimum and maximum character pairs (which are K characters apart). Now for each pair of characters, we check if there is a subsequence of S containing only characters between the minimum and maximum characters (inclusive) and whether both the minimum and maximum characters are present in the subsequence. If both conditions are met, and the length of the current subsequence is greater than the length of and(which is basically a variable to store the length of the longest subsequence), then the current subsequence becomes the new ans.

The steps involved in this approach are as follows:

  • First, we calculate the length of the input string ‘S’ and initialize an empty string ‘ans‘ where we store the longest subsequence found.
  • Then we iterate a loop from 0 to (26 – K), where 26 is the total number of alphabets. So, this loop will run for all possible min, max character pairs with a difference of ‘K’.
  • Inside the loop, we define two variables ‘min_char’ and ‘max_char’ that represent the current minimum and maximum character pair being considered. We also initialize an empty string ‘curr’ that will be used to store the current subsequence being considered.
  • We set two boolean flags ‘min_char_exists‘ and ‘max_char_exists‘ to false, which will be used to check if the current subsequence contains the minimum and maximum character.
  • We start another loop that runs from 0 to the length of the input string ‘S’. Inside the loop, we check if the current character lies between the minimum and maximum characters or is equal to them. If it does, we add the character to the ‘curr’ string and turn the flags ‘min_char_exists’ or ‘max_char_exists’ true if the current character is equal to the minimum or maximum character, respectively.
  • After the inner loop completes, we check if both ‘min_char_exists’ and ‘max_char_exists’ are true and if the length of the ‘curr’ string is greater than the length of the ‘ans’ string found so far. If both conditions are true, we update the ‘ans’ string with the current ‘curr’ string.
  • At last, we return the ‘ans’ string.

Below is the code for the above approach:

C++

#include <bits/stdc++.h>

using namespace std;

  

string longest_subseq(string S, int k)

{

    int n = S.size();

    string ans = "";

  

    

    

    

    

    for (int i = 0; i < 26 - k; i++) {

  

        

        

        char min_char = 'a' + i;

        char max_char = min_char + k;

        string curr = "";

  

        

        

        

        bool min_char_exists = false,

             max_char_exists = false;

        for (int j = 0; j < n; j++) {

  

            

            

            

            if (S[j] >= min_char && S[j] <= max_char) {

  

                

                

                

                if (S[j] == min_char)

                    min_char_exists = true;

                if (S[j] == max_char)

                    max_char_exists = true;

                curr += S[j];

            }

        }

  

        

        

        

        

        if (min_char_exists && max_char_exists

            && curr.size() > ans.size())

            ans = curr;

    }

    return ans;

}

  

int main()

{

    string s = "daaaabbbadddddeeee";

    int k = 1;

  

    

    cout << longest_subseq(s, k);

    return 0;

}

Time Complexity: O(n)
Auxiliary Space: O(n)


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given a string S consisting of lowercase letters. Find the longest subsequence of S such that the difference between the maximum and minimum occurring characters in the subsequence is exactly K.

Examples:

Input: S = ‘abcdeg’ and K = 4
Output: abcde

Input: S = ‘daaaabbbadddddeeee’, K = 1
Output: ddddddeeee

Approach: This can be solved with the following idea:

Iterate through all possible minimum and maximum character pairs (which are K characters apart). Now for each pair of characters, we check if there is a subsequence of S containing only characters between the minimum and maximum characters (inclusive) and whether both the minimum and maximum characters are present in the subsequence. If both conditions are met, and the length of the current subsequence is greater than the length of and(which is basically a variable to store the length of the longest subsequence), then the current subsequence becomes the new ans.

The steps involved in this approach are as follows:

  • First, we calculate the length of the input string ‘S’ and initialize an empty string ‘ans‘ where we store the longest subsequence found.
  • Then we iterate a loop from 0 to (26 – K), where 26 is the total number of alphabets. So, this loop will run for all possible min, max character pairs with a difference of ‘K’.
  • Inside the loop, we define two variables ‘min_char’ and ‘max_char’ that represent the current minimum and maximum character pair being considered. We also initialize an empty string ‘curr’ that will be used to store the current subsequence being considered.
  • We set two boolean flags ‘min_char_exists‘ and ‘max_char_exists‘ to false, which will be used to check if the current subsequence contains the minimum and maximum character.
  • We start another loop that runs from 0 to the length of the input string ‘S’. Inside the loop, we check if the current character lies between the minimum and maximum characters or is equal to them. If it does, we add the character to the ‘curr’ string and turn the flags ‘min_char_exists’ or ‘max_char_exists’ true if the current character is equal to the minimum or maximum character, respectively.
  • After the inner loop completes, we check if both ‘min_char_exists’ and ‘max_char_exists’ are true and if the length of the ‘curr’ string is greater than the length of the ‘ans’ string found so far. If both conditions are true, we update the ‘ans’ string with the current ‘curr’ string.
  • At last, we return the ‘ans’ string.

Below is the code for the above approach:

C++

#include <bits/stdc++.h>

using namespace std;

  

string longest_subseq(string S, int k)

{

    int n = S.size();

    string ans = "";

  

    

    

    

    

    for (int i = 0; i < 26 - k; i++) {

  

        

        

        char min_char = 'a' + i;

        char max_char = min_char + k;

        string curr = "";

  

        

        

        

        bool min_char_exists = false,

             max_char_exists = false;

        for (int j = 0; j < n; j++) {

  

            

            

            

            if (S[j] >= min_char && S[j] <= max_char) {

  

                

                

                

                if (S[j] == min_char)

                    min_char_exists = true;

                if (S[j] == max_char)

                    max_char_exists = true;

                curr += S[j];

            }

        }

  

        

        

        

        

        if (min_char_exists && max_char_exists

            && curr.size() > ans.size())

            ans = curr;

    }

    return ans;

}

  

int main()

{

    string s = "daaaabbbadddddeeee";

    int k = 1;

  

    

    cout << longest_subseq(s, k);

    return 0;

}

Time Complexity: O(n)
Auxiliary Space: O(n)

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