Skip to content
Decoder

Greatest Common Divisor of Strings

Easy

Problem

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.

Example 1

Input
str1 = "ABCABC", str2 = "ABC"
Output
"ABC"

Example 2

Input
str1 = "ABABAB", str2 = "ABAB"
Output
"AB"

Example 3

Input
str1 = "LEET", str2 = "CODE"
Output
""

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.