forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1451.java
34 lines (32 loc) · 1.12 KB
/
_1451.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
public class _1451 {
public static class Solution1 {
public String arrangeWords(String text) {
TreeMap<Integer, List<String>> map = new TreeMap<>();
String[] words = text.split(" ");
for (String word : words) {
int len = word.length();
if (!map.containsKey(len)) {
map.put(len, new ArrayList<>());
}
map.get(len).add(word.toLowerCase());
}
StringBuilder sb = new StringBuilder();
boolean first = true;
for (int len : map.keySet()) {
List<String> strings = map.get(len);
for (String str : strings) {
if (first) {
str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
first = false;
}
sb.append(str + " ");
}
}
return sb.substring(0, sb.length() - 1);
}
}
}