Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | Java program to implement the above approach ; Iterate through the length if we could find a [ i ] = = b [ j ] we could increment I and decrement j ; Else we could just break the loop as its not a palindrome type sequence ; We could concatenate the a ' s ▁ left ▁ part ▁ + b ' s right part in a variable a and a ' s ▁ right ▁ part + b ' s left in the variable b ; We would check for the palindrome condition if yes we print True else False ; Driver Code
import java . util . * ; import java . io . * ; class GFG { public static String rev ( String str ) { int st = 0 ; int ed = str . length ( ) - 1 ; char [ ] s = str . toCharArray ( ) ; while ( st < ed ) { char temp = s [ st ] ; s [ st ] = s [ ed ] ; s [ ed ] = temp ; st ++ ; ed -- ; } return ( s . toString ( ) ) ; } public static boolean check ( String a , String b , int n ) { int i = 0 ; int j = n - 1 ; while ( i < n ) { if ( a . charAt ( i ) != b . charAt ( j ) ) break ; i += 1 ; j -= 1 ; String xa = a . substring ( i , j + 1 ) ; String xb = b . substring ( i , j + 1 ) ; StringBuffer XA = new StringBuffer ( xa ) ; XA . reverse ( ) ; StringBuffer XB = new StringBuffer ( xb ) ; XB . reverse ( ) ; if ( xa == XA . toString ( ) || xb == XB . toString ( ) ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String a = " xbdef " ; String b = " cabex " ; if ( check ( a , b , a . length ( ) ) || check ( b , a , a . length ( ) ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }