📌  相关文章
📜  检查一个字符串是否是另一个给定字符串的连接

📅  最后修改于: 2021-10-25 09:13:45             🧑  作者: Mango

给定两个长度分别为NM 的字符串str1str2 ,任务是检查字符串str1 是否可以通过重复连接字符串str2来形成。

例子:

处理方法:按照以下步骤解决问题:

  • 遍历字符串str1str2
  • 对于str1str2 的每个字符,检查str1[i] == str2[i % M]是否。
  • 如果发现任何字符为假,则打印“否”
  • 否则,打印“是”

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check if a string is
// concatenation of another string
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 Code
int main()
{
    string str1 = "abcabcabc";
    string str2 = "abc";
 
    if (checkConcat(str1, str2)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to check if a String is
// concatenation of another String
static 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 Code
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");
    }
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to check if a is
# concatenation of another string
def 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 Code
if __name__ == '__main__':
     
    str1 = "abcabcabc"
    str2 = "abc"
 
    if (checkConcat(str1, str2)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to check if a String is
// concatenation of another String
static 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 Code
public 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


Javascript


输出
Yes

时间复杂度: O(N)
辅助空间: O(1)