Repeated subsequence of length 2 or more | C ++ program to check if any repeated subsequence exists in the string ; A function to check if a string str is palindrome ; l and h are leftmost and rightmost corners of str Keep comparing characters while they are same ; The main function that checks if repeated subsequence exists in the string ; Find length of input string ; Create an array to store all characters and their frequencies in str [ ] ; Traverse the input string and store frequencies of all characters in freq [ ] array . ; If the character count is more than 2 we found a repetition ; In - place remove non - repeating characters from the string ; check if the resultant string is palindrome ; special case - if length is odd return true if the middle character is same as previous one ; return false if string is a palindrome ; return true if string is not a palindrome ; Driver code
#include <bits/stdc++.h> NEW_LINE #define MAX_CHAR  256 NEW_LINE using namespace std ; bool isPalindrome ( char str [ ] , int l , int h ) { while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false ; return true ; } int check ( char str [ ] ) { int n = strlen ( str ) ; int freq [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str [ i ] ] ++ ; if ( freq [ str [ i ] ] > 2 ) return true ; } int k = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( freq [ str [ i ] ] > 1 ) str [ k ++ ] = str [ i ] ; str [ k ] = ' \0' ; if ( isPalindrome ( str , 0 , k - 1 ) ) { if ( k & 1 ) return str [ k / 2 ] == str [ k / 2 - 1 ] ; return false ; } return true ; } int main ( ) { char str [ ] = " ABCABD " ; if ( check ( str ) ) cout << " Repeated ▁ Subsequence ▁ Exists " ; else cout << " Repeated ▁ Subsequence ▁ Doesn ' t ▁ Exists " ; return 0 ; }