Check if a Palindromic String can be formed by concatenating Substrings of two given Strings | Java program to implement the above approach ; Function to check if a palindromic string can be formed from the substring of given strings ; Boolean array to mark presence of characters ; Check if any of the character of str2 is already marked ; If a common character is found ; If no common character is found ; Driver code
import java . util . Arrays ; class GFG { public static boolean check ( String str1 , String str2 ) { boolean [ ] mark = new boolean [ 26 ] ; Arrays . fill ( mark , false ) ; int n = str1 . length ( ) , m = str2 . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mark [ str1 . charAt ( i ) - ' a ' ] = true ; } for ( int i = 0 ; i < m ; i ++ ) { if ( mark [ str2 . charAt ( i ) - ' a ' ] ) return true ; } return false ; } public static void main ( String [ ] args ) { String str1 = " abca " , str2 = " efad " ; if ( check ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }