Java Straight Forward Methods


  • 0
    Z

    Enumerate each case.
    In preprocessing, compare each string and its reverse and remain the greater one.

    public class Solution {
        String max = "a";
        public String splitLoopedString(String[] strs) {
            if (strs==null || strs.length==0) return "";
            for (int i = 0; i < strs.length; i++) {
                String reverse = new StringBuilder(strs[i]).reverse().toString();
                if (strs[i].compareTo(reverse) < 0) strs[i] = reverse;
            }
            
            for (int i = 0; i < strs.length; i++) {
                findMax(strs, i, true);
                findMax(strs, i, false);
            }
            
            return max;
        }
        
        void findMax(String[] strs, int i, boolean flag) {
            if (strs[i]==null || strs[i].length()==0) return;
            String str = strs[i];
            if (flag) str = new StringBuilder(str).reverse().toString();
            String rst1 = "";
            String rst2 = "";
            for (int k = i+1; k < strs.length; k++) {
                rst1 = rst1 + strs[k];
            }
            
            for (int k = 0; k < i; k++) {
                rst2 = rst2 + strs[k];
            }
            
            for (int j = 0; j< strs[i].length(); j++) {
                String cand = str.substring(j) + rst1 + rst2 + str.substring(0, j);
                if (max.compareTo(cand) < 0) max = cand;
            }
        } 
    }
    

Log in to reply
 

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