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"
    }
}

package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/rearrange-words-in-a-sentence/
 */
public class Problem1451 {
    public String arrangeWords(String text) {
        String[] split = text.toLowerCase().split(" ");
        Word[] words = new Word[split.length];
        for (int i = 0; i < words.length; i++) {
            words[i] = new Word(split[i], i);
        }
        Arrays.sort(words, (a, b) -> {
            if (a.word.length() < b.word.length()) {
                return -1;
            } else if (a.word.length() > b.word.length()) {
                return 1;
            }
            return Integer.compare(a.index, b.index);
        });
        StringBuilder answer = new StringBuilder();
        for (int i = 0; i < words.length; i++) {
            if (i == 0) {
                answer.append(("" + words[i].word.charAt(0)).toUpperCase() + words[i].word.substring(1));
            } else {
                answer.append(" ");
                answer.append(words[i].word);
            }
        }
        return answer.toString();
    }

    private static class Word {
        private final String word;
        private final int index;

        public Word(String word, int index) {
            this.word = word;
            this.index = index;
        }
    }
}

Solve problem 1451