69ms python solution (beats 100%)


  • 0
    class Solution(object):
        def splitLoopedString(self, strs):
            strs = [max(s, s[::-1]) for s in strs]
            ans = init = "".join(strs)
            c = max(ans)
            start = 0
            for s in strs:
                if c in s:
                    front, tail = init[:start], init[start + len(s):]
                    for cur in [s, s[::-1]]:
                        index = -1
                        for i in xrange(cur.count(c)):
                            index = cur.index(c, index + 1)
                            candidate = cur[index:] + tail + front + cur[:index]
                            ans = max(ans, candidate)
                start += len(s)
            return ans
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.