Problem · String

Greatest Common Divisor of Strings

Learn this problem
EasyAtlassian logoAtlassianFULLTIMEOA

Problem statement

A non-empty string t divides s when s is one or more concatenated copies of t.

Return the longest string that divides both str1 and str2, or the empty string when none exists.

Function

gcdOfStrings(str1: String, str2: String) → String

Examples

Example 1

str1 = "ABCABC"str2 = "ABC"return = "ABC"

"ABC" repeats twice to form the first string and once to form the second.

Constraints

  • 1 <= str1.length, str2.length <= 100000
  • Both strings contain uppercase English letters.

More Atlassian problems

drafts saved locally
public String gcdOfStrings(String str1, String str2) {
  // Write your code here.
}
str1"ABCABC"
str2"ABC"
expected"ABC"
checking account