package leetcode;

/**
 * https://leetcode.com/problems/rearrange-words-in-a-sentence/
 */
public class Problem1451 {
    public String arrangeWords(String text) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem1451 prob = new Problem1451();
        System.out.println(prob.arrangeWords("Leetcode is cool")); // "Is cool leetcode"
        System.out.println(prob.arrangeWords("Keep calm and code on")); // "On and keep calm code"
        System.out.println(prob.arrangeWords("To be or not to be")); // "To be or to be not"
    }
}

@@ -12,16 +12,42 @@
 tcode;%0A%0A
+import java.util.Arrays;%0A%0A
 /**%0A * h
@@ -195,415 +195,1112 @@
     
-// TODO%0A        return null;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1451 prob = new Problem1451();%0A        System.out.println(prob.arrangeWords(%22Leetcode is cool%22)); // %22Is cool leetcode%22%0A        System.out.println(prob.arrangeWords(%22Keep calm and code on%22)); // %22On and keep calm code%22%0A        System.out.println(prob.arrangeWords(%22To be or not to be%22)); // %22To be or to be not%22
+String%5B%5D split = text.toLowerCase().split(%22 %22);%0A        Word%5B%5D words = new Word%5Bsplit.length%5D;%0A        for (int i = 0; i %3C words.length; i++) %7B%0A            words%5Bi%5D = new Word(split%5Bi%5D, i);%0A        %7D%0A        Arrays.sort(words, (a, b) -%3E %7B%0A            if (a.word.length() %3C b.word.length()) %7B%0A                return -1;%0A            %7D else if (a.word.length() %3E b.word.length()) %7B%0A                return 1;%0A            %7D%0A            return Integer.compare(a.index, b.index);%0A        %7D);%0A        StringBuilder answer = new StringBuilder();%0A        for (int i = 0; i %3C words.length; i++) %7B%0A            if (i == 0) %7B%0A                answer.append((%22%22 + words%5Bi%5D.word.charAt(0)).toUpperCase() + words%5Bi%5D.word.substring(1));%0A            %7D else %7B%0A                answer.append(%22 %22);%0A                answer.append(words%5Bi%5D.word);%0A            %7D%0A        %7D%0A        return answer.toString();%0A    %7D%0A%0A    private static class Word %7B%0A        private final String word;%0A        private final int index;%0A%0A        public Word(String word, int index) %7B%0A            this.word = word;%0A            this.index = index;%0A        %7D
 %0A   

Solve problem 1451