Find the GCD of an array made up of numeric strings | JAVA program for the above approach ; Recursive function to return gcd of A and B ; Base case ; Length of A is greater ; Calculate GCD ; Store the GCD of the length of the Strings ; Driver Code ; Function call
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 ) ) ; } }