给定一个字符串数组arr [] ,该任务是给定的字符串数组的最大公约数。
In strings ‘A’ and ‘B’, we say “B divides A” if and only if A = concatenation of B more than 1 times.Find the largest string which divides both A and B.
例子:
Input: arr[] = { “GFGGFG”, “GFGGFG”, “GFGGFGGFGGFG” }
Output: “GFGGFG”
Explanation:
“GFGGFG” is the largest string which divides the whole array elements.
Input: arr = { “Geeks”, = “GFG”}
Output: “”
方法:想法是使用递归。步骤如下:
- 创建一个递归函数gcd(str1,str2) 。
- 如果str2的长度大于str1,那么我们将使用gcd(str2,str1)进行递归。
- 现在,如果str1不是以str2开头,则返回一个空字符串。
- 如果更长的字符串用较短的字符串开始,切断长字符串并易复发或重复的共同前缀部分,直到一个是空的。
- 在上述步骤之后返回的字符串是给定字符串数组的gcd。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that finds gcd of 2 strings
string gcd(string str1, string str2)
{
// If str1 length is less than
// that of str2 then recur
// with gcd(str2, str1)
if (str1.length() < str2.length())
{
return gcd(str2, str1);
}
// If str1 is not the
// concatenation of str2
else if(str1.find(str2) != 0)
{
return "";
}
else if (str2 == "")
{
// GCD string is found
return str1;
}
else
{
// Cut off the common prefix
// part of str1 & then recur
return gcd(str1.substr(str2.length()), str2);
}
}
// Function to find GCD of array of
// strings
string findGCD(string arr[], int n)
{
string result = arr[0];
for (int i = 1; i < n; i++)
{
result = gcd(result, arr[i]);
}
// Return the GCD of strings
return result;
}
// Driver Code
int main()
{
// Given array of strings
string arr[]={ "GFGGFG",
"GFGGFG",
"GFGGFGGFGGFG" };
int n = sizeof(arr)/sizeof(arr[0]);
// Function Call
cout << findGCD(arr, n);
}
// This code is contributed by pratham76.
Java
// Java program for the above approach
class GCD {
// Function that finds gcd of 2 strings
static String gcd(String str1, String str2)
{
// If str1 length is less than
// that of str2 then recur
// with gcd(str2, str1)
if (str1.length() < str2.length()) {
return gcd(str2, str1);
}
// If str1 is not the
// concatenation of str2
else if (!str1.startsWith(str2)) {
return "";
}
else if (str2.isEmpty()) {
// GCD string is found
return str1;
}
else {
// Cut off the common prefix
// part of str1 & then recur
return gcd(str1.substring(str2.length()),
str2);
}
}
// Function to find GCD of array of
// strings
static String findGCD(String arr[], int n)
{
String result = arr[0];
for (int i = 1; i < n; i++) {
result = gcd(result, arr[i]);
}
// Return the GCD of strings
return result;
}
// Driver Code
public static void
main(String[] args)
{
// Given array of strings
String arr[]
= new String[] { "GFGGFG",
"GFGGFG",
"GFGGFGGFGGFG" };
int n = arr.length;
// Function Call
System.out.println(findGCD(arr, n));
}
}
Python3
# Python3 program for the above approach
# Function that finds gcd of 2 strings
def gcd(str1, str2):
# If str1 length is less than
# that of str2 then recur
# with gcd(str2, str1)
if(len(str1) < len(str2)):
return gcd(str2, str1)
# If str1 is not the
# concatenation of str2
elif(not str1.startswith(str2)):
return ""
elif(len(str2) == 0):
# GCD string is found
return str1
else:
# Cut off the common prefix
# part of str1 & then recur
return gcd(str1[len(str2):], str2)
# Function to find GCD of array of
# strings
def findGCD(arr, n):
result = arr[0]
for i in range(1, n):
result = gcd(result, arr[i])
# Return the GCD of strings
return result
# Driver Code
# Given array of strings
arr = ["GFGGFG", "GFGGFG", "GFGGFGGFGGFG" ]
n = len(arr)
# Function Call
print(findGCD(arr, n))
# This code is contributed by avanitrachhadiya2155
C#
// C# program for
// the above approach
using System;
class GFG{
// Function that finds gcd
// of 2 strings
static String gcd(String str1,
String str2)
{
// If str1 length is less than
// that of str2 then recur
// with gcd(str2, str1)
if (str1.Length < str2.Length)
{
return gcd(str2, str1);
}
// If str1 is not the
// concatenation of str2
else if (!str1.StartsWith(str2))
{
return "";
}
else if (str2.Length == 0)
{
// GCD string is found
return str1;
}
else
{
// Cut off the common prefix
// part of str1 & then recur
return gcd(str1.Substring(str2.Length),
str2);
}
}
// Function to find GCD
// of array of strings
static String findGCD(String []arr,
int n)
{
String result = arr[0];
for (int i = 1; i < n; i++)
{
result = gcd(result, arr[i]);
}
// Return the GCD of strings
return result;
}
// Driver Code
public static void Main(String[] args)
{
// Given array of strings
String []arr = new String[] {"GFGGFG",
"GFGGFG",
"GFGGFGGFGGFG"};
int n = arr.Length;
// Function Call
Console.WriteLine(findGCD(arr, n));
}
}
// This code is contributed by shikhasingrajput
输出:
GFGGFG
时间复杂度: O(N * log(B)),其中N是字符串的数量,而B是arr []中任何字符串的最大长度。
辅助空间: O(1)