Greatest Common Divisor of Strings
EasyStringsmath
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Say that a string x *divides* a string t if t equals x repeated one or more times.
Given two strings str1 and str2, return the **longest** string x that divides both of them. If no such string exists, return the empty string "".
Examples
Input: str1 = "ABCABC", str2 = "ABC"
Output: "ABC"
"ABC" x2 = "ABCABC" and "ABC" x1 = "ABC".
Input: str1 = "ABABAB", str2 = "ABAB"
Output: "AB"
"AB" divides both; "ABAB" does not divide "ABABAB".
Input: str1 = "LEET", str2 = "CODE"
Output: ""
The strings share no repeating unit.
Constraints
- 1 <= str1.length, str2.length <= 1000
- `str1` and `str2` consist of uppercase English letters
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.