Frequency of maximum occurring subsequence in given string | C ++ program for the above approach ; Function to find the frequency ; freq stores frequency of each english lowercase character ; dp [ i ] [ j ] stores the count of subsequence with ' a ' + i and ' a ' + j character ; Increment the count of subsequence j and s [ i ] ; Update the frequency array ; For 1 length subsequence ; For 2 length subsequence ; Return the final result ; Driver Code ; Given string str ; Function Call
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; ll findCount ( string s ) { ll freq [ 26 ] ; ll dp [ 26 ] [ 26 ] ; memset ( freq , 0 , sizeof freq ) ; memset ( dp , 0 , sizeof dp ) ; for ( int i = 0 ; i < s . size ( ) ; ++ i ) { for ( int j = 0 ; j < 26 ; j ++ ) { dp [ j ] [ s [ i ] - ' a ' ] += freq [ j ] ; } freq [ s [ i ] - ' a ' ] ++ ; } ll ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) ans = max ( freq [ i ] , ans ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { ans = max ( dp [ i ] [ j ] , ans ) ; } } return ans ; } int main ( ) { string str = " acbab " ; cout << findCount ( str ) ; return 0 ; }