You are given an array of strings strs. You can concatenate the strings together to form a loop, where for each string you can choose to reverse it or not. Among all the possible loops, find the lexicographically largest string you can get after cutting the loop, which turns the looped string into a regular one.
Specifically, building the string has two phases:
-
Concatenate all the strings into a loop, reversing some of them or not, and connecting them in the same order as given.
-
Cut the loop at one breakpoint anywhere, which turns it into a regular string starting from the character at the cut point.
Return the lexicographically largest string you can make after all the reversing and cutting.
Function Signature
def split_looped_string(strs: List[str]) -> str:
Rules
-
The strings keep their given order. Only the direction of each string can change, and each string is reversed or kept independently of the others.
-
The loop closes from the end of the last string back to the start of the first, so the returned string always contains every character exactly once and its length is the total length of all strings.
-
The cut can fall inside a string or between two strings.
-
Strings are compared lexicographically, character by character.
Constraints
-
1 <= strs.length <= 1000
-
1 <= strs[i].length <= 1000
-
1 <= sum(strs[i].length) <= 1000
-
strs[i]
consists of lowercase English letters.
Examples
Input: strs = ["abc", "xyz"]
Output: "zyxcba"
The possible loops are "abcxyz", "abczyx", "cbaxyz" and "cbazyx". The answer has to start with "z". Cutting "cbazyx" just before the "z" gives "zyxcba", which beats the other strings that start with "z" ("zyxabc", "zabcxy" and "zcbaxy").
Input: strs = ["abc"]
Output: "cba"
With a single string, reversing it to "cba" and cutting before its first character gives "cba". Every other rotation of "abc" or "cba" is smaller.
Input: strs = ["azb", "c"]
Output: "zbca"
The answer has to start with the "z" in the middle of the first string. Keeping "azb" as it is puts "b" right after the "z" and gives "zbca", while reversing it to "bza" puts "a" after the "z" and gives only "zacb". So the string holding the cut keeps its original direction even though "bza" is the larger of its two forms.