Repeated subsequence of length 2 or more | Javascript 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 ; It is checked so that StringIndexOutOfBounds can be avoided ; return false if string is a palindrome ; return true if string is not a palindrome ; Driver Code
let MAX_CHAR = 256 ; function isPalindrome ( str , l , h ) { while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false ; return true ; } function check ( str ) { let n = str . length ; let freq = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) ] ++ ; if ( freq [ str [ i ] . charCodeAt ( 0 ) ] > 2 ) return true ; } let k = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( freq [ str [ i ] . charCodeAt ( 0 ) ] > 1 ) str . replace ( str [ k ++ ] , str [ i ] ) ; str . replace ( str [ k ] , ' \0 ' ) ; if ( isPalindrome ( str , 0 , k - 1 ) ) { if ( ( k & 1 ) == 1 ) { if ( k / 2 >= 1 ) return ( str [ Math . floor ( k / 2 ) ] == str [ Math . floor ( k / 2 ) - 1 ] ) ; } return false ; } return true ; } let str = " " ; if ( check ( str ) ) document . write ( " " ) ; else document . write ( " " + " " ) ;