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
function gcd ( str1 , str2 ) { if ( str1 . length < str2 . length ) { return gcd ( str2 , str1 ) ; } else if ( ! str1 . startsWith ( str2 ) ) { return " " ; } else if ( str2 . length == 0 ) { return str1 ; } else { return gcd ( str1 . substr ( str2 . length ) , str2 ) ; } } function findGCD ( arr , n ) { let result = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { result = gcd ( result , arr [ i ] ) ; } return result ; } let arr = [ " " , " " , " " ] ; let n = arr . length ; document . write ( findGCD ( arr , n ) ) ;