Find the GCD of an array made up of numeric strings
Given an array arr[] consisting of numeric strings, the task is to calculate Greatest Common Divisor of the given array.
Considering strings ‘A’ and ‘B’, “B divides A” if and only if A is a concatenation of B more than once. Find the largest string which divides both A and B.
Examples:
Input: arr[] = { “GFGGFG”, “GFGGFGGFGGFG” }
Output: “GFGGFG”
Explanation:
“GFGGFG” is the largest string which divides the whole array elements.
Input: arr = { “Geeks”, “GFG”}
Output: “”
Approach: Follow the steps below to solve the problem:
- Calculate GCD of the length of all the strings of the given array, say GCD.
- Check if all the strings of the given array can be made by concatenating the substring {arr[0][0], .., arr[0][GCD – 1]} any number of times or not. If found to be true, then print {arr[0][0], .., arr[0][GCD – 1]}.
- Otherwise, print an empty string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int GCD( int lena, int lenb)
{
if (lena == 0)
return lenb;
if (lenb == 0)
return lena;
if (lena == lenb)
return lena;
if (lena > lenb)
return GCD(lena - lenb, lenb);
return GCD(lena, lenb - lena);
}
string StringGCD(string a, string b)
{
int gcd = GCD(a.size(), b.size());
if (a.substr(0, gcd) == b.substr(0, gcd))
{
int x = (( int )b.size()/gcd);
int y = (( int )a.size()/gcd);
string r= "" ,s= "" ;
while (x--) s += a;
while (y--) r += b;
if (s == r)
return a.substr(0, gcd);
}
return "-1" ;
}
int main()
{
string a = "geeksgeeks" ;
string b = "geeks" ;
cout<<(StringGCD(a, b));
}
|
Java
import java.util.*;
class GFG
{
static int GCD( int lena, int lenb)
{
if (lena == 0 )
return lenb;
if (lenb == 0 )
return lena;
if (lena == lenb)
return lena;
if (lena > lenb)
return GCD(lena - lenb, lenb);
return GCD(lena, lenb - lena);
}
static String StringGCD(String a, String b)
{
int gcd = GCD(a.length(), b.length());
if (a.substring( 0 , gcd).equals(b.substring( 0 , gcd)))
{
int x = (( int )b.length()/gcd);
int y = (( int )a.length()/gcd);
String r= "" ,s= "" ;
while (x-- > 0 ) s += a;
while (y-- > 0 ) r += b;
if (s.equals(r))
return a.substring( 0 , gcd);
}
return "-1" ;
}
public static void main(String[] args)
{
String a = "geeksgeeks" ;
String b = "geeks" ;
System.out.print(StringGCD(a, b));
}
}
|
Python3
def GCD(lena, lenb):
if (lena = = 0 ):
return lenb
if (lenb = = 0 ):
return lena
if (lena = = lenb):
return lena
if (lena > lenb):
return GCD(lena - lenb, lenb)
return GCD(lena, lenb - lena)
def StringGCD(a, b):
gcd = GCD( len (a), len (b))
if a[:gcd] = = b[:gcd]:
if a * ( len (b) / / gcd) = = b * ( len (a) / / gcd):
return a[:gcd]
return - 1
a = 'geeksgeeks'
b = 'geeks'
print (StringGCD(a, b))
|
C#
using System;
class GFG
{
static int GCD( int lena, int lenb)
{
if (lena == 0)
return lenb;
if (lenb == 0)
return lena;
if (lena == lenb)
return lena;
if (lena > lenb)
return GCD(lena - lenb, lenb);
return GCD(lena, lenb - lena);
}
static String StringGCD(String a, String b)
{
int gcd = GCD(a.Length, b.Length);
if (a.Substring(0, gcd).Equals(b.Substring(0, gcd)))
{
int x = (( int )b.Length/gcd);
int y = (( int )a.Length/gcd);
String r= "" , s= "" ;
while (x-- >0) s += a;
while (y-- >0) r += b;
if (s.Equals(r))
return a.Substring(0, gcd);
}
return "-1" ;
}
public static void Main(String[] args)
{
String a = "geeksgeeks" ;
String b = "geeks" ;
Console.Write(StringGCD(a, b));
}
}
|
Javascript
<script>
function GCD(lena,lenb)
{
if (lena == 0)
return lenb;
if (lenb == 0)
return lena;
if (lena == lenb)
return lena;
if (lena > lenb)
return GCD(lena - lenb, lenb);
return GCD(lena, lenb - lena);
}
function StringGCD(a,b)
{
let gcd = GCD(a.length, b.length);
if (a.substring(0, gcd) == (b.substring(0, gcd)))
{
let x = Math.floor(b.length/gcd);
let y = Math.floor(a.length/gcd);
let r= "" ,s= "" ;
while (x-- >0)
s += a;
while (y-- >0)
r += b;
if (s == (r))
return a.substring(0, gcd);
}
return "-1" ;
}
let a = "geeksgeeks" ;
let b = "geeks" ;
document.write(StringGCD(a, b));
</script>
|
Time Complexity: O(N * M), where M is the length of strings
Auxiliary Space: O(1)
Last Updated :
25 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...