Math Fundamentals18 sections · 814 units
Open in Course

Problem - Greatest Common Divisor of Strings

LeetCode 1071

You are given two strings str1 and str2. A string t divides both if you can form str1 by concatenating t some number of times, and same for str2.

Return the longest string that divides both str1 and str2. If no such string exists, return empty string.

Example: str1 = ABABABAB, str2 = ABAB. Answer is ABAB because ABAB repeated 22 times gives ABABABAB and ABAB repeated 11 time gives ABAB.