Skip to content

Greatest Common Divisor of Strings

EasyBit Manipulation & Math
Decode it first. Clues, constraints, pattern and plan before any code.

A string t divides a string s if s is t repeated one or more times ("ABAB" is "AB" twice). Given str1 and str2, return the largest string that divides both of them, or "" if none does.

str1 = "ABCABC", str2 = "ABC"   →  "ABC"
str1 = "ABABAB", str2 = "ABAB"  →  "AB"
str1 = "LEET",   str2 = "CODE"  →  ""

Constraints

  • 1 <= str1.length, str2.length <= 1000
  • str1 and str2 consist of uppercase English letters.