📜  两个字符串的公共基本字符串数

📅  最后修改于: 2022-05-13 01:57:07.418000             🧑  作者: Mango

两个字符串的公共基本字符串数

给定两个字符串s1 和 s2,我们需要找到两个的公共基本字符串的数量。如果子字符串的重复连接产生 s,则字符串s 的子字符串称为基本字符串。
例子:

Input : s1 = "pqrspqrs"
        s2 = "pqrspqrspqrspqrs"
Output : 2
The two common base strings are "pqrs"
and "pqrspqrs".

Input: s1 = "bbb"
       s2 = "bb"
Output: 1
There is only one common base string
which is "b". 

公共基本字符串的最大可能长度等于两个字符串中较小的长度。所以我们运行一个循环,考虑较小字符串的所有前缀,并为每个前缀检查它是否是一个公共基础。
以下是以下方法的实现

C++
// CPP program to count common base strings
// of s1 and s2.
#include 
using namespace std;
 
// function for finding common divisor .
bool isCommonBase(string base, string s1, 
                               string s2)
{
    // Checking if 'base' is base string 
    // of 's1'
    for (int j = 0; j < s1.length(); ++j)
        if (base[j % base.length()] != s1[j])
            return false;
     
    // Checking if 'base' is base string
    // of 's2'
    for (int j = 0; j < s2.length(); ++j)
        if (base[j % base.length()] != s2[j])
            return false;   
 
    return true;
}
 
int countCommonBases(string s1, string s2) {
   int n1 = s1.length(), n2 = s2.length();
   int count = 0;
   for (int i=1; i<=min(n1, n2); i++)
   {
       string base = s1.substr(0, i);
       if (isCommonBase(base, s1, s2))
           count++;
   }
   return count;
}
 
// Driver code
int main()
{
    string s1 = "pqrspqrs";
    string s2 = "pqrspqrspqrspqrs";
    cout << countCommonBases(s1, s2) << endl;
    return 0;
}


Java
// Java program to count common
// base strings of s1 and s2.
 
class GFG
{
 
// function for finding common divisor
static boolean isCommonBase(String base,
                            String s1,
                            String s2)
{
    // Checking if 'base' is base
    // String of 's1'
    for (int j = 0; j < s1.length(); ++j)
    {
        if (base.charAt(j %
            base.length()) != s1.charAt(j))
        {
            return false;
        }
    }
 
    // Checking if 'base' is base
    // String of 's2'
    for (int j = 0; j < s2.length(); ++j)
    {
        if (base.charAt(j %
            base.length()) != s2.charAt(j))
        {
            return false;
        }
    }
 
    return true;
}
 
static int countCommonBases(String s1,
                            String s2)
{
    int n1 = s1.length(),
        n2 = s2.length();
    int count = 0;
    for (int i = 1; i <= Math.min(n1, n2); i++)
    {
        String base = s1.substring(0, i);
        if (isCommonBase(base, s1, s2))
        {
            count++;
        }
    }
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    String s1 = "pqrspqrs";
    String s2 = "pqrspqrspqrspqrs";
 
    System.out.println(countCommonBases(s1, s2));
}
}
 
// This code is contributed by Rajput-JI


Python 3
# Python 3 program to count common
# base strings of s1 and s2.
 
# function for finding common divisor .
def isCommonBase(base, s1, s2):
 
    # Checking if 'base' is base
    # string of 's1'
    for j in range(len(s1)):
        if (base[j % len(base)] != s1[j]):
            return False
     
    # Checking if 'base' is base
    # string of 's2'
    for j in range(len(s2)):
        if (base[j % len( base)] != s2[j]):
            return False
 
    return True
 
def countCommonBases(s1, s2):
    n1 = len(s1)
    n2 = len(s2)
    count = 0
    for i in range(1, min(n1, n2) + 1):
        base = s1[0: i]
        if (isCommonBase(base, s1, s2)):
            count += 1
         
    return count
 
# Driver code
if __name__ == "__main__":
     
    s1 = "pqrspqrs"
    s2 = "pqrspqrspqrspqrs"
    print(countCommonBases(s1, s2))
 
# This code is contributed by ita_c


C#
// C# program to count common base
// strings of s1 and s2.
using System;
 
class GFG
{
// function for finding common divisor
static bool isCommonBase(String Base,
                         String s1,
                         String s2)
{
    // Checking if 'base' is base
    // String of 's1'
    for (int j = 0; j < s1.Length; ++j)
    {
        if (Base[j % Base.Length] != s1[j])
        {
            return false;
        }
    }
 
    // Checking if 'base' is base
    // String of 's2'
    for (int j = 0; j < s2.Length; ++j)
    {
        if (Base[j % Base.Length] != s2[j])
        {
            return false;
        }
    }
 
    return true;
}
 
static int countCommonBases(String s1,
                            String s2)
{
    int n1 = s1.Length, n2 = s2.Length;
    int count = 0;
    for (int i = 1;
             i <= Math.Min(n1, n2); i++)
    {
        String Base = s1.Substring(0, i);
        if (isCommonBase(Base, s1, s2))
        {
            count++;
        }
    }
    return count;
}
 
// Driver code
public static void Main()
{
    String s1 = "pqrspqrs";
    String s2 = "pqrspqrspqrspqrs";
 
    Console.Write(countCommonBases(s1, s2));
}
}
 
// This code is contributed by Rajput-JI


PHP


Javascript


输出:
2

时间复杂度: O(min(n1, n2) * (n1 + n2))