Find a palindromic string B such that given String A is a subsequense of B | Java program to find a palindromic string B such that given String A is a subsequense of B ; Function to check if a string is palindrome ; Reversing a string ; check if reversed string is equal to given string ; Function to find a palindromic string B such that given String A is a subsequense of B ; Reversing the string A ; If the string A is already a palindrome return A ; else return B ; Swap values of left and right ; Driver Code
class GFG { static boolean checkPalindrome ( String s ) { String x = reverse ( s ) ; return x . equals ( s ) ; } static String findStringB ( String A ) { String B = reverse ( A ) ; B = B + A ; if ( checkPalindrome ( A ) ) { return A ; } return B ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . valueOf ( temparray ) ; } public static void main ( String [ ] args ) { String A = " ab " ; System . out . println ( findStringB ( A ) ) ; } }