1071. Greatest Common Divisor of Strings

#string

Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.

String t divides s if and only if s = t + t + t + ... + t + t (i.e., t is concatenated with itself one or more times)


Intuition

Approach

Complexity

Space Complexity
Time Complexity

O()\text{O}()

O()\text{O}()

Code

Alternative

  • Select the smaller string

  • Iterate over all the sub-string possible

    • If the sub-string repeated string.lenght()/subString.length() is equal to the target strings its a divisor

  • If there is a common divisor the strings are formed by repeating some string so we can add them in any order and still get the same result.

Last updated