Split Concatenated Strings

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def splitLoopedString(self, strs: List[str]) -> str:
        B = []
        for x in strs:  # Use strs instead of A
            reversed_x = x[::-1]
            max_string = max(x, reversed_x)
            B.append(max_string)

        ans = ""
        for i, token in enumerate(B):
            for start in (token, token[::-1]):
                for j in range(len(start) + 1):
                    candidate = start[j:] + "".join(B[i+1:] + B[:i]) + start[:j]
                    if candidate > ans:
                        ans = candidate
        return ans