Smallest string divisible by two given strings | Function to calculate GCD of two numbers ; Function to calculate LCM of two numbers ; Function to find the smallest string which is divisible by strings S and T ; Store the length of both strings ; Store LCM of n and m ; Temporary strings to store concatenated strings ; Concatenate s1 ( l / n ) times ; Concatenate t1 ( l / m ) times ; If s1 and t1 are equal ; Otherwise , print - 1 ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function lcm ( a , b ) { return ( a / gcd ( a , b ) ) * b ; } function findSmallestString ( s , t ) { let n = s . length , m = t . length ; let l = lcm ( n , m ) ; let s1 = " " , t1 = " " ; for ( let i = 0 ; i < l / n ; i ++ ) { s1 += s ; } for ( let i = 0 ; i < l / m ; i ++ ) { t1 += t ; } if ( s1 == ( t1 ) ) { document . write ( s1 + " " ) ; } else { document . write ( - 1 + " " ) ; } } let S = " " , T = " " ; findSmallestString ( S , T ) ;