Check if a string is concatenation of another given string | Java program to implement the above approach ; Function to check if a String is concatenation of another String ; Stores the length of str2 ; Stores the length of str1 ; If M is not multiple of N ; Traverse both the Strings ; If str1 is not concatenation of str2 ; Driver Code
import java . util . * ; class GFG { static boolean checkConcat ( String str1 , String str2 ) { int N = str1 . length ( ) ; int M = str2 . length ( ) ; if ( N % M != 0 ) { return false ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( i % M ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str1 = " abcabcabc " ; String str2 = " abc " ; if ( checkConcat ( str1 , str2 ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }