Neat Java Solution


  • 5
    public class Solution {
        public String splitLoopedString(String[] strs) {
            for (int i = 0; i < strs.length; i++) {
                String rev = new StringBuilder(strs[i]).reverse().toString();
                if (strs[i].compareTo(rev) < 0)
                    strs[i] = rev;
            }
            String res = "";
            for (int i = 0; i < strs.length; i++) {
                String rev = new StringBuilder(strs[i]).reverse().toString();
                for (String st: new String[] {strs[i], rev}) {
                    for (int k = 0; k < st.length(); k++) {
                        StringBuilder t = new StringBuilder(st.substring(k));
                        for (int j = i + 1; j < strs.length; j++)
                            t.append(strs[j]);
                        for (int j = 0; j < i; j++)
                            t.append(strs[j]);
                        t.append(st.substring(0, k));
                        if (t.toString().compareTo(res) > 0)
                            res = t.toString();
                    }
                }
            }
            return res;
        }
    }

  • 9

    @vinod23 Optimization: only test the max characters. i.e. if z appeared in some of those strings, final result has to starts with z. So we just test all those positions. This will improve run time from ~200ms to ~100ms.

    public class Solution {
        public String splitLoopedString(String[] strs) {
            char max = 'a';
            for (int i = 0; i < strs.length; i++) {
                for (char c : strs[i].toCharArray()) {
                    if (c > max) max = c;
                }
                String rev = new StringBuilder(strs[i]).reverse().toString();
                if (strs[i].compareTo(rev) < 0) strs[i] = rev;
            }
            
            String res = "";
            for (int i = 0; i < strs.length; i++) {
                String rev = new StringBuilder(strs[i]).reverse().toString();
                for (String st: new String[] {strs[i], rev}) {
                    for (int k = 0; k < st.length(); k++) {
                        if (st.charAt(k) != max) continue;
                        StringBuilder t = new StringBuilder(st.substring(k));
                        for (int j = i + 1; j < strs.length; j++)
                            t.append(strs[j]);
                        for (int j = 0; j < i; j++)
                            t.append(strs[j]);
                        t.append(st.substring(0, k));
                        if (t.toString().compareTo(res) > 0)
                            res = t.toString();
                    }
                }
            }
            return res;
        }
    }
    

  • 0

    You guys solutions are amazingly neat and logical!
    a little bit improvement by adding a judge for the max char before the loop cause no need for judging two times, can save 5ms

    public String splitLoopedString(String[] strs) {
            char max = 'a';
            for (int i = 0; i < strs.length; i++) {
                for (char c : strs[i].toCharArray()) {
                    if (c > max) max = c;
                }
                String rev = new StringBuilder(strs[i]).reverse().toString();
                if (strs[i].compareTo(rev) < 0) strs[i] = rev;
            }
            
            String res = "";
            for (int i = 0; i < strs.length; i++) {
                if(!strs[i].contains("" + max)) continue; //here judge before
                String rev = new StringBuilder(strs[i]).reverse().toString();
                for (String st: new String[] {strs[i], rev}) {
                    for (int k = 0; k < st.length(); k++) {
                        if (st.charAt(k) != max) continue;
                        StringBuilder t = new StringBuilder(st.substring(k));
                        for (int j = i + 1; j < strs.length; j++)
                            t.append(strs[j]);
                        for (int j = 0; j < i; j++)
                            t.append(strs[j]);
                        t.append(st.substring(0, k));
                        if (t.toString().compareTo(res) > 0)
                            res = t.toString();
                    }
                }
            }
            return res;
        }

Log in to reply
 

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