📌  相关文章
📜  检查一个字符串可以重复以制作另一个字符串

📅  最后修改于: 2021-04-28 14:17:07             🧑  作者: Mango

给定两个字符串ab ,任务是检查字符串a可以重复多少次以生成字符串b 。如果不能通过重复a生成b ,则打印-1

例子:

方法:

  • 如果len(b)%len(a)!= 0,则不能通过重复a来将-1打印为b
  • 否则设置count = len(b)/ len(a)并重复一次计数次数。
    • 如果a = b,则打印计数
    • 否则打印-1

下面是上述方法的实现:

C++
// CPP implementation of the approach
#include
using namespace std;
  
// Function to return the count of repetitions 
// of string a to generate string b
int getCount(string a, string b)
{
  
    // If b cannot be generated by repeating a
    if(b.length() % a.length() != 0)
        return -1;
      
    int count = b.length() /a.length();
  
    // Repeat a count number of times
    string str = "";
    for(int i = 0; i < count; i++)
    {
        str = str + a;
    }
    if(str == b)
        return count;
      
    return -1; 
}
  
// Driver code
int main()
{
    string a = "geeks";
    string b = "geeksgeeks";
    cout << (getCount(a, b));
    return 0;
}
  
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach 
  
class GfG
{
  
    // Function to return the count of repetitions 
    // of string a to generate string b 
    static int getCount(String a, String b) 
    { 
        // If b cannot be generated by repeating a 
        if(b.length() % a.length() != 0) 
            return -1; 
          
        int count = b.length() / a.length(); 
      
        // Repeat a count number of times 
        String str = ""; 
        for(int i = 0; i < count; i++) 
        { 
            str = str + a; 
        }
          
        if(str.equals(b)) 
            return count; 
          
        return -1; 
    } 
  
    // Driver code
    public static void main(String []args)
    {
        String a = "geeks"; 
        String b = "geeksgeeks"; 
        System.out.println(getCount(a, b));
    }
}
      
// This code is contributed by Rituraj Jain


Python 3
# Python3 implementation of the approach
  
# Function to return the count of repetitions 
# of string a to generate string b
def getCount(a, b):
      
    # If b cannot be generated by repeating a
    if(len(b) % len(a) != 0):
        return -1;
      
    count = int(len(b) / len(a))
      
    # Repeat a count number of times
    a = a * count
      
    if(a == b):
        return count
      
    return -1;
  
# Driver code
if __name__ == '__main__':
    a = 'geeks'
    b = 'geeksgeeks'
    print(getCount(a, b))


C#
// C# implementation of the approach 
using System;
  
class GfG
{
  
    // Function to return the count of repetitions 
    // of string a to generate string b 
    static int getCount(String a, String b) 
    { 
        // If b cannot be generated by repeating a 
        if(b.Length % a.Length != 0) 
            return -1; 
          
        int count = b.Length / a.Length; 
      
        // Repeat a count number of times 
        String str = ""; 
        for(int i = 0; i < count; i++) 
        { 
            str = str + a; 
        }
          
        if(str.Equals(b)) 
            return count; 
          
        return -1; 
    } 
  
    // Driver code
    public static void Main(String []args)
    {
        String a = "geeks"; 
        String b = "geeksgeeks"; 
        Console.WriteLine(getCount(a, b));
    }
}
  
// This code contributed by Rajput-Ji


PHP


输出:
2