Thursday, August 18, 2022
HomeWordPress DevelopmentLongest Subsequence with identical char as substrings and distinction of frequency at...

Longest Subsequence with identical char as substrings and distinction of frequency at most Ok


#embrace <bits/stdc++.h>

utilizing namespace std;

  

int maxLen = 0;

  

bool isContinuous(string& temp)

{

    unordered_map<char, int> last_Pos;

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

        if (last_Pos[temp[i]]) {

            if (i - last_Pos[temp[i]] + 1 <= 1)

                last_Pos[temp[i]] = i + 1;

            else

                return 0;

        }

        else

            last_Pos[temp[i]] = i + 1;

    }

    return 1;

}

  

void generateSubSequences(string& str, string temp, int j,

                          int& okay)

{

    unordered_map<char, int> freq;

    if (j == str.size()) {

        if (temp.size() > 0) {

            int minfreq = INT_MAX, maxfreq = INT_MIN;

            freq.clear();

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

                freq[temp[i]]++;

  

            for (auto& x : freq) {

                minfreq = min(minfreq, x.second);

                maxfreq = max(maxfreq, x.second);

            }

  

            if (maxfreq - minfreq <= okay

                && isContinuous(temp))

                maxLen = max(maxLen, (int)temp.size());

        }

        return;

    }

    generateSubSequences(str, temp, j + 1, okay);

    temp.push_back(str[j]);

    generateSubSequences(str, temp, j + 1, okay);

}

  

int primary()

{

    string str = "abba", temp;

    int okay = 1;

    generateSubSequences(str, temp, 0, okay);

    cout << maxLen;

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments