Split Concatenated Strings

https://www.lintcode.com/problem/split-concatenated-strings/description

import java.util.List;

public class Solution {
    /**
     * @param strs: a list of string
     * @return: return a string
     */
    public String splitLoopedString(List strs) {
        // write your code here
//        最后必然是反转一个,所以对于其它的都是进行取最大的。
        for (int i = 0; i < strs.size(); i++) {
            String rev = new StringBuilder(strs.get(i)).reverse().toString();
            if (strs.get(i).compareTo(rev) < 0) {
                strs.set(i, rev);
            }
        }
        String res = "a";
        for (int i = 0; i < strs.size(); i++) {
            String rev = new StringBuilder(strs.get(i)).reverse().toString();
            for (String str2 : new String[]{strs.get(i), rev}) {
                for (int k = 0; k < str2.length(); k++) {
                    if (str2.charAt(k) >= res.charAt(0)) { //optimize OJ time from 200ms to 96ms  
                        StringBuilder sb = new StringBuilder(str2.substring(k));
                        for (int j = i + 1; j < strs.size(); j++) {
                            sb.append(strs.get(j));
                        }
                        for (int j = 0; j < i; j++) {
                            sb.append(strs.get(j));
                        }
                        sb.append(str2.substring(0, k));
                        if (res.compareTo(sb.toString()) < 0) {
                            res = sb.toString();
                        }
                    }
                }
            }
        }
        return res;
    }

}

你可能感兴趣的:(Split Concatenated Strings)