15% off all plans & roadmaps Use code
See plansProblem editorial
A string x divides another string y when y can be formed by concatenating one or more copies of x. A common divisor string of str1 and str2 must divide both parameters.
Greatest Common Divisor of Strings is a String Manipulation Patterns problem, in the Repeated Substring Pattern Detection subpattern. The pattern covers palindrome checking, anagram detection, pattern matching, and string conversion
The reference solution runs in O(|str1| + |str2|) time and O(|str1| + |str2|) auxiliary space space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Greatest Common Divisor of Strings is a String Manipulation Patterns problem, in the Repeated Substring Pattern Detection subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.