> ## 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.

# Split Concatenated Strings Python Solution

> Tested Python solution for LeetCode 555 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 555, [Medium](/catalog/medium). Topics: [Greedy](/catalog/topics/greedy), [Array](/catalog/topics/array), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/split-concatenated-strings/description/).

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

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

## Problem

You are given an array of strings `strs`. You could concatenate these strings together into a loop, where for each string, you could choose to reverse it or not. Among all the possible loops

Return the lexicographically largest string after cutting the loop, which will make the looped string into a regular one.

Specifically, to find the lexicographically largest string, you need to experience two phases:

1. Concatenate all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given.
2. Cut and make one breakpoint in any place of the loop, which will make the looped string into a regular one starting from the character at the cutpoint.

And your job is to find the lexicographically largest one among all the possible regular strings.

### Examples

```
Input: strs = ["abc","xyz"]
Output: "zyxcba"
Explanation: You can get the looped string "-abcxyz-", "-abczyx-", "-cbaxyz-", "-cbazyx-", where '-' represents the looped status. The answer string came from the fourth looped one, where you could cut from the middle character 'a' and get "zyxcba".
```

```
Input: strs = ["abc"]
Output: "cba"
```

### Constraints

* `1 <= strs.length <= 1000`
* `1 <= strs[i].length <= 1000`
* `1 <= sum(strs[i].length) <= 1000`
* `strs[i]` consists of lowercase English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(total length^2) worst case from cut-point candidates
    # Space: O(total length)
    def split_looping_string(self, strs: list[str]) -> str:
        n = len(strs)
        best_parts = [max(s, s[::-1]) for s in strs]
        best = ""
        for i in range(n):
            left = "".join(best_parts[:i])
            right = "".join(best_parts[i + 1 :])
            for t in (strs[i], strs[i][::-1]):
                for k in range(len(t)):
                    cand = t[k:] + right + left + t[:k]
                    if cand > best:
                        best = cand
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(total length^2) worst case from cut-point candidates | O(total length) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.