Split Concatenated Strings Problem
Description
LeetCode Problem 555.
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:
- Concatenate all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given.
- 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.
Example 1:
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".
Example 2:
Input: strs = ["abc"]
Output: "cba"
Constraints:
1 <= strs.length <= 10001 <= strs[i].length <= 10001 <= sum(strs[i].length) <= 1000strs[i]consists of lowercase English letters.
Solution
Approach: Greedy + Try Every Cut
The answer string starts at the cut point, goes once around the loop and ends just before the cut. So the cut lies inside exactly one string, strs[i]; every other string appears whole, in the original loop order starting right after strs[i].
For the strings that are not cut, the choice is easy: each one occupies a fixed block of positions in the result, so making each block as large as possible makes the whole string as large as possible. Replace every string by the larger of itself and its reverse.
The cut string is different. Which way it faces changes where the result begins, so we cannot decide it in advance. For every index i:
- Build
rest, the concatenation ofstrs[i+1:]followed bystrs[:i](already in their best orientation). - For both orientations
wofstrs[i]and every split pointj, the candidate isw[j:] + rest + w[:j]. Keep the largest.
The tricky part is the cut string: its larger orientation is not always the right one. In ["abz","z","zba"], the best answer "zzzbaab" cuts "abz" (the smaller way) just before its last z, so that the string starts with three zs. Trying both orientations of the cut string covers this.
Complexity: With L the total length of all strings, there are O(L) candidates and each is built and compared in O(L), so O(L2) time and O(L) extra space.
class Solution:
def splitLoopedString(self, strs: List[str]) -> str:
# Every string we do not cut through should face its larger way.
strs = [max(s, s[::-1]) for s in strs]
ans = ""
for i, s in enumerate(strs):
rest = "".join(strs[i + 1:]) + "".join(strs[:i])
for w in (s, s[::-1]): # the cut string can face either way
for j in range(len(w)):
ans = max(ans, w[j:] + rest + w[:j])
return ans