Formatted question description: https://leetcode.ca/all/555.html

555. Split Concatenated Strings

Level

Medium

Description

Given a list of strings, 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, you need to find the lexicographically biggest string after cutting the loop, which will make the looped string into a regular one.

Specifically, to find the lexicographically biggest 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 biggest one among all the possible regular strings.

Example:

Input: “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”.

Note:

  1. The input strings will only contain lowercase letters.
  2. The total length of all the strings will not over 1,000.

Solution

If the split takes place inside a string, then only this string and its reversed string should be both tried, and for the remaining strings, a string that is lexicographically bigger will always lead to a lexicographically bigger result. So first loop over strs and for each string, reverse it if the reversed string is lexicographically bigger than the original string.

Then for each string in strs, try all possible positions of splitting and form each concatenated string with each splitting position, and return the lexicographically biggest string.

class Solution {
    public String splitLoopedString(String[] strs) {
        int length = strs.length;
        for (int i = 0; i < length; i++) {
            String str = strs[i];
            String str2 = new StringBuffer(str).reverse().toString();
            if (str2.compareTo(str) > 0)
                strs[i] = str2;
        }
        String biggestStr = "";
        for (int i = 0; i < length; i++) {
            String str = strs[i];
            String str2 = new StringBuffer(str).reverse().toString();
            String[] tempArray = {str, str2};
            for (String curStr : tempArray) {
                int curLength = curStr.length();
                for (int j = 0; j < curLength; j++) {
                    StringBuffer sb = new StringBuffer(curStr.substring(j));
                    int index = (i + 1) % length;
                    for (int k = 1; k < length; k++) {
                        sb.append(strs[index]);
                        index = (index + 1) % length;
                    }
                    sb.append(curStr.substring(0, j));
                    String curTotalStr = sb.toString();
                    if (curTotalStr.compareTo(biggestStr) > 0)
                        biggestStr = curTotalStr;
                }
            }
        }
        return biggestStr;
    }
}

All Problems

All Solutions