Program to find Greatest Common Divisor (GCD) of N strings
Given an array of string arr[], the task is the Greatest Common Divisor of the given array of string.
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.
Examples:
Input: arr[] = { “GFGGFG”, “GFGGFG”, “GFGGFGGFGGFG” }
Output: “GFGGFG”
Explanation:
“GFGGFG” is the largest string which divides the whole array elements.
Input: arr = { “Geeks”, = “GFG”}
Output: “”
Approach: The idea is to use recursion. Below are the steps:
- Create a recursive function gcd(str1, str2).
- If the length of str2 is more than str1 then we will recur with gcd(str2, str1).
- Now if str1 doesn’t start with str2 then return an empty string.
- If the longer string begins with a shorter string, cut off the common prefix part of the longer string and recur or repeat until one is empty.
- The string returned after the above steps are the gcd of the given array of string.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include<bits/stdc++.h>using namespace std;// Function that finds gcd of 2 stringsstring 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// stringsstring 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 Codeint 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 approachclass 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 stringsdef 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# stringsdef 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 stringsarr = ["GFGGFG", "GFGGFG", "GFGGFGGFGGFG" ]n = len(arr)# Function Callprint(findGCD(arr, n))# This code is contributed by avanitrachhadiya2155 |
C#
// C# program for// the above approachusing System;class GFG{// Function that finds gcd// of 2 stringsstatic 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 stringsstatic 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 Codepublic 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 |
Javascript
<script> // Javascript program for the above approach // Function that finds gcd // of 2 strings function gcd(str1, 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.substr(str2.length), str2); } } // Function to find GCD // of array of strings function findGCD(arr, n) { let result = arr[0]; for (let i = 1; i < n; i++) { result = gcd(result, arr[i]); } // Return the GCD of strings return result; } // Given array of strings let arr = ["GFGGFG", "GFGGFG", "GFGGFGGFGGFG"]; let n = arr.length; // Function Call document.write(findGCD(arr, n)); // This code is contributed by decode2207.</script> |
GFGGFG
Time Complexity: O(N*log(B)), where N is the number of strings, and B is the maximum length of any string in arr[].
Auxiliary Space: O(1)

