📜  计算给定字符串的公共除数的数量

📅  最后修改于: 2021-04-21 22:46:34             🧑  作者: Mango

给定两个字符串AB,任务是计算两个字符串的公约数的数目。一个字符串s是字符串T的除数如果t可通过重复的次数来产生。

例子:

方法:要使字符串s成为字符串t的候选除数,必须满足以下条件:

  1. s必须是t的前缀。
  2. len(t)%len(s)= 0

初始化计数= 0和从所述第一字符作为前缀的结束字符开始,检查前缀划分的长度两个字符串的长度是否与也如果前缀是在两个字符串相同。如果是,则更新count = count +1 。对所有可能的前缀重复这些步骤。最后打印计数值

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if sub-string
// s[0...k] is repeated a number of times
// to generate string s
int check(string s, int k)
{
    for (int i = 0; i < s.length(); i++)
        if (s[i] != s[i % k])
            return false;
  
    return true;
}
  
// Function to return the count of common divisors
int countCommonDivisors(string a, string b)
{
    int ct = 0;
    int n = a.size(), m = b.size();
    for (int i = 1; i <= min(n, m); i++) {
  
        // If the length of the sub-string
        // divides length of both the strings
        if (n % i == 0 && m % i == 0)
  
            // If prefixes match in both the strings
            if (a.substr(0, i) == b.substr(0, i))
  
                // If both the strings can be generated
                // by repeating the current prefix
                if (check(a, i) && check(b, i))
                    ct++;
    }
    return ct;
}
  
// Driver code
int main()
{
    string a = "xaxa", b = "xaxaxaxa";
  
    cout << countCommonDivisors(a, b);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG 
{
  
    // Function that returns true if sub-string 
    // s[0...k] is repeated a number of times 
    // to generate String s 
    static boolean check(String s, int k)
    {
        for (int i = 0; i < s.length(); i++) 
        {
            if (s.charAt(i) != s.charAt(i % k))
            {
                return false;
            }
        }
        return true;
    }
  
    // Function to return the
    // count of common divisors 
    static int countCommonDivisors(String a, String b) 
    {
        int ct = 0;
        int n = a.length(), m = b.length();
        for (int i = 1; i <= Math.min(n, m); i++) 
        {
  
            // If the length of the sub-string 
            // divides length of both the strings 
            if (n % i == 0 && m % i == 0)  
            {
                // If prefixes match in both the strings
                if (a.substring(0, i).equals(b.substring(0, i))) 
                // by repeating the current prefix 
                {
                    // If both the strings can be generated 
                    if (check(a, i) && check(b, i)) 
                    {
                        ct++;
                    }
                }
            }
        }
        return ct;
    }
  
    // Driver code 
    public static void main(String[] args) 
    {
        String a = "xaxa", b = "xaxaxaxa";
        System.out.println(countCommonDivisors(a, b));
    }
} 
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the above approach
  
# Function that returns true if sub-string 
# s[0...k] is repeated a number of times 
# to generate String s 
def check(s, k):
  
    for i in range (0, len(s)): 
      
        if (s[i] != s[i % k]):
          
            return False
          
    return True
  
# Function to return the
# count of common divisors 
def countCommonDivisors(a, b): 
  
    ct = 0
    n = len(a)
    m = len(b)
    for i in range(1, min(n, m) + 1): 
      
        # If the length of the sub-string 
        # divides length of both the strings 
        if (n % i == 0 and m % i == 0):
          
            # If prefixes match in both the strings
            if (a[0 : i] == b[0 : i]) :
                  
                # by repeating the current prefix 
              
                # If both the strings can be generated 
                if (check(a, i) and check(b, i)) :
                  
                    ct = ct + 1
      
    return ct
      
# Driver code 
a = "xaxa"
b = "xaxaxaxa"
print(countCommonDivisors(a, b))
  
# This code is contributed by ihritik


C#
// C# implementation of the above approach
using System;
  
class GFG 
{
  
// Function that returns true if sub-string 
// s[0...k] is repeated a number of times 
// to generate String s 
static bool check(string s, int k)
{
    for (int i = 0; i < s.Length; i++) 
    {
        if (s[i] != s[i % k])
        {
            return false;
        }
    }
    return true;
}
  
// Function to return the count of
// common divisors 
static int countCommonDivisors(string a, 
                               string b) 
{
    int ct = 0;
    int n = a.Length, m = b.Length;
    for (int i = 1; 
             i <= Math.Min(n, m); i++) 
    {
  
        // If the length of the sub-string 
        // divides length of both the strings 
        if (n % i == 0 && m % i == 0) 
        {
            // If prefixes match in both the strings
            if (a.Substring(0, i) == (b.Substring(0, i))) 
              
            // by repeating the current prefix 
            {
                // If both the strings can be generated 
                if (check(a, i) && check(b, i)) 
                {
                    ct++;
                }
            }
        }
    }
    return ct;
}
  
// Driver code 
public static void Main() 
{
    string a = "xaxa", b = "xaxaxaxa";
    Console.WriteLine(countCommonDivisors(a, b));
}
} 
  
// This code is contributed by ihritik


输出:
2