Check if the string has a reversible equal substring at the ends | Function to print longest substring that appears at beginning of string and also at end in reverse order ; Stores the resultant string ; If the characters are same ; Otherwise , break ; If the string can 't be formed ; Otherwise print resultant string ; Driver Code
function commonSubstring ( s ) { let n = s . length ; let i = 0 ; let j = n - 1 ; let ans = " " ; while ( j >= 0 ) { if ( s [ i ] == s [ j ] ) { ans += s [ i ] ; i ++ ; j -- ; } else { break ; } } if ( ans . length == 0 ) document . write ( " " ) ; else { document . write ( " " + " " + ans ) ; } } let S = " " ; commonSubstring ( S ) ;