Straight forward java solution. Beat 90%


  • 0
    4
    public class Solution {
        public String splitLoopedString(String[] strs) {
            char max = 'a';
            StringBuilder sb = new StringBuilder();
            
            for (int i = 0; i < strs.length; i++) {
                String reversed = new StringBuilder(strs[i]).reverse().toString();
                strs[i] = (reversed.compareTo(strs[i]) > 0 ? reversed : strs[i]);
                sb.append(strs[i]);
                 for (int j = 0; j < strs[i].length(); j++) {
                    max = max < strs[i].charAt(j) ? strs[i].charAt(j) : max;
                }
            }
            String s = sb.toString();
            
             int cur = 0;
             String rst = "";
             for (int i = 0; i < strs.length; i++) {
                String s1 = strs[i];
                String s2 = new StringBuilder(s1).reverse().toString();
                cur += (i - 1 >= 0 ? strs[i - 1].length() : 0);
                String middle = s.substring(cur + s1.length(), s.length()) + s.substring(0, cur);
                for (int j = 0; j < s1.length(); j++) {
                    String temp = "";
                    if(s1.charAt(j) == max) {
                        temp = s1.substring(j) + middle + s1.substring(0, j);
                        rst = (rst.compareTo(temp) >= 0 ? rst : temp);
                    }
                    if(s2.charAt(j) == max) {
                        temp = s2.substring(j) + middle + s2.substring(0, j);
                        rst = (rst.compareTo(temp) >= 0 ? rst : temp);
                    }
                }
            }
            return rst;
        }
    }
    

Log in to reply
 

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