Search
⌘K

Leetcode 1071. Greatest Common Divisor of Strings

Find the longest string x such that both str1 and str2 are formed by repeating x one or more times. Key insight: x must be a prefix whose length divides both string lengths, so check the prefix of length gcd(len(str1), len(str2)) and verify it repeats to form both strings.


Question Timeline

See when this question was last asked and where, including any notes left by other candidates.

Comments

Your account is free and you can post anonymously if you choose.