Check if a string is concatenation of another given string - GeeksforGeeks

18 May, 2021
Given two strings str1 and str2 of length N and M respectively, the task is to check if the string str1 can be formed by concatenating the string str2 repetitively or not.
Examples:
Input: str1 = “abcabcabc”, str2 = “abc”Output: YesExplanation: Concatenating the string str2 thrice generates the string (“abc” + “abc” + “abc” = ) “abcabcabc”.Therefore, the required output is Yes.
Input: str1 = “abcabcab”, str2 = “abc”Output: No
Approach: Follow the steps below to solve the problem:
Traverse the strings str1 and str2.
For each character of str1 and str2, check if str1[i] == str2[i % M] or not.
If found to be false for any character, print “No”.
Otherwise, print “Yes”.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to check if a string is// concatenation of another stringbool checkConcat(string str1,                 string str2){     // Stores the length of str2    int N = str1.length();     // Stores the length of str1    int M = str2.length();     // If M is not multiple of N    if (N % M != 0) {        return false;    }     // Traverse both the strings    for (int i = 0; i < N; i++) {         // If str1 is not concatenation        // of str2        if (str1[i] != str2[i % M]) {            return false;        }    }    return true;} // Driver Codeint main(){    string str1 = "abcabcabc";    string str2 = "abc";     if (checkConcat(str1, str2)) {        cout << "Yes";    }    else {        cout << "No";    }}
// Java program to implement// the above approachimport java.util.*; class GFG{ // Function to check if a String is// concatenation of another Stringstatic boolean checkConcat(String str1,                           String str2){         // Stores the length of str2    int N = str1.length();     // Stores the length of str1    int M = str2.length();     // If M is not multiple of N    if (N % M != 0)    {        return false;    }     // Traverse both the Strings    for(int i = 0; i < N; i++)    {                 // If str1 is not concatenation        // of str2        if (str1.charAt(i) !=            str2.charAt(i % M))        {            return false;        }    }    return true;} // Driver Codepublic static void main(String[] args){    String str1 = "abcabcabc";    String str2 = "abc";     if (checkConcat(str1, str2))    {        System.out.print("Yes");    }    else    {        System.out.print("No");    }}} // This code is contributed by Amit Katiyar
# Python3 program to implement# the above approach # Function to check if a is# concatenation of another stringdef checkConcat(str1, str2):     # Stores the length of str2    N = len(str1)     # Stores the length of str1    M = len(str2)     # If M is not multiple of N    if (N % M != 0):        return False     # Traverse both the strings    for i in range(N):         # If str1 is not concatenation        # of str2        if (str1[i] != str2[i % M]):            return False                 return True # Driver Codeif __name__ == '__main__':         str1 = "abcabcabc"    str2 = "abc"     if (checkConcat(str1, str2)):        print("Yes")    else:        print("No") # This code is contributed by mohit kumar 29
// C# program to implement// the above approachusing System;class GFG{ // Function to check if a String is// concatenation of another Stringstatic bool checkConcat(String str1,                        String str2){  // Stores the length  // of str2  int N = str1.Length;   // Stores the length  // of str1  int M = str2.Length;   // If M is not multiple  // of N  if (N % M != 0)  {    return false;  }   // Traverse both the Strings  for(int i = 0; i < N; i++)  {    // If str1 is not    // concatenation of str2    if (str1[i] !=        str2[i % M])    {      return false;    }  }  return true;} // Driver Codepublic static void Main(String[] args){  String str1 = "abcabcabc";  String str2 = "abc";   if (checkConcat(str1, str2))  {    Console.Write("Yes");  }  else  {    Console.Write("No");  }}} // This code is contributed by 29AjayKumar
<script>// javascript program for the// above approach // Function to check if a String is// concatenation of another Stringfunction checkConcat(str1, str2){          // Stores the length of str2    let N = str1.length;      // Stores the length of str1    let M = str2.length;      // If M is not multiple of N    if (N % M != 0)    {        return false;    }      // Traverse both the Strings    for(let i = 0; i < N; i++)    {                  // If str1 is not concatenation        // of str2        if (str1[i] !=            str2[i % M])        {            return false;        }    }    return true;}  // Driver Code     let str1 = "abcabcabc";    let str2 = "abc";      if (checkConcat(str1, str2))    {        document.write("Yes");    }    else    {        document.write("No");    }           </script>
Yes
Time Complexity: O(N) Auxiliary Space: O(1) 
mohit kumar 29
amit143katiyar
29AjayKumar
chinmoy1997pal
Greedy
School Programming
Searching
Strings
Searching
Strings
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program for First Fit algorithm in Memory Management
Optimal Page Replacement Algorithm
Program for Best Fit algorithm in Memory Management
Bin Packing Problem (Minimize number of used Bins)
Program for Worst Fit algorithm in Memory Management
Python Dictionary
Arrays in C/C++
Reverse a string in Java
Inheritance in C++
Constructors in C++