Program to find Greatest Common Divisor ( GCD ) of N strings | Function that finds gcd of 2 strings ; If str1 length is less than that of str2 then recur with gcd ( str2 , str1 ) ; If str1 is not the concatenation of str2 ; GCD string is found ; Cut off the common prefix part of str1 & then recur ; Function to find GCD of array of strings ; Return the GCD of strings ; Given array of strings ; Function Call
def gcd ( str1 , str2 ) : NEW_LINE INDENT if ( len ( str1 ) < len ( str2 ) ) : NEW_LINE INDENT return gcd ( str2 , str1 ) NEW_LINE DEDENT elif ( not str1 . startswith ( str2 ) ) : NEW_LINE INDENT return " " NEW_LINE DEDENT elif ( len ( str2 ) == 0 ) : NEW_LINE INDENT return str1 NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( str1 [ len ( str2 ) : ] , str2 ) NEW_LINE DEDENT DEDENT def findGCD ( arr , n ) : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT result = gcd ( result , arr [ i ] ) NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ " GFGGFG " , " GFGGFG " , " GFGGFGGFGGFG " ] NEW_LINE n = len ( arr ) NEW_LINE print ( findGCD ( arr , n ) ) NEW_LINE