Count maximum occurrence of subsequence in string such that indices in subsequence is in A . P . | C ++ implementation to find the maximum occurrence of the subsequence such that the indices of characters are in arithmetic progression ; Function to find the maximum occurrence of the subsequence such that the indices of characters are in arithmetic progression ; Frequency for characters ; Loop to count the occurrence of ith character before jth character in the given string ; Increase the frequency of s [ i ] or c of string ; Maximum occurrence of subsequence of length 1 in given string ; Maximum occurrence of subsequence of length 2 in given string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumOccurrence ( string s ) { int n = s . length ( ) ; int freq [ 26 ] = { 0 } ; int dp [ 26 ] [ 26 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { int c = ( s [ i ] - ' a ' ) ; for ( int j = 0 ; j < 26 ; j ++ ) dp [ j ] += freq [ j ] ; freq ++ ; } int answer = INT_MIN ; for ( int i = 0 ; i < 26 ; i ++ ) answer = max ( answer , freq [ i ] ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { answer = max ( answer , dp [ i ] [ j ] ) ; } } return answer ; } int main ( ) { string s = " xxxyy " ; cout << maximumOccurrence ( s ) ; return 0 ; }