> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Greatest Common Divisor of Strings

> Tested Python solution for LeetCode 1071 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1071, Easy. Topics: Math, String. [View on LeetCode](https://leetcode.com/problems/gcd-of-strings/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1071   # by problem number
lcpy gen -s gcd_of_strings   # by problem name
```

## Problem

For two strings `s` and `t`, we say "`t` divides `s`" if and only if `s = t + t + t + ... + t + t` (i.e., `t` is concatenated with itself one or more times).

Given two strings `str1` and `str2`, return *the largest string* `x` *such that* `x` *divides both* `str1` *and* `str2`.

### Examples

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

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

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/gcd_of_strings/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/gcd_of_strings/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from math import gcd


class Solution:
    # Time: O(n + m) — concatenate + compare strings of total length n + m
    # Space: O(n + m) — concatenated strings
    def gcd_of_strings(self, str1: str, str2: str) -> str:
        # If a common divisor exists, str1 and str2 must compose the same
        # string regardless of concatenation order.
        if str1 + str2 != str2 + str1:
            return ""

        # The largest common divisor has length = gcd of the two lengths.
        gcd_len = gcd(len(str1), len(str2))
        return str1[:gcd_len]
```

## Complexity

| Time                                                           | Space                           |
| -------------------------------------------------------------- | ------------------------------- |
| O(n + m) — concatenate + compare strings of total length n + m | O(n + m) — concatenated strings |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
