Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 830 Bytes

Question_1451.md

File metadata and controls

27 lines (22 loc) · 830 Bytes

LeetCode Records - Question 1451 Rearrange Words in a Sentence

Attempt 1: Use split() to get the String[]

class Solution {
    public String arrangeWords(String text) {
        String[] splits = text.split("\s");
        splits[0] = splits[0].toLowerCase();

        Arrays.sort(splits, (str1, str2) -> str1.length() - str2.length());

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(Character.toUpperCase(splits[0].charAt(0)));
        stringBuilder.append(splits[0].substring(1));
        for (int i = 1; i < splits.length; i++) {
            stringBuilder.append(' ');
            stringBuilder.append(splits[i]);
        }

        return stringBuilder.toString();
    }
}
  • Runtime: 17 ms (Beats: 100.00%)
  • Memory: 45.60 MB (Beats: 46.02%)