Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 250767f

Browse files
add 1451
1 parent 88f9efb commit 250767f

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1451|[Rearrange Words in a Sentence](https://leetcode.com/problems/rearrange-words-in-a-sentence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1451.java) | |Medium|String, Sort|
1112
|1450|[Number of Students Doing Homework at a Given Time](https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1450.java) | |Easy|Array|
1213
|1448|[Count Good Nodes in Binary Tree](https://leetcode.com/problems/count-good-nodes-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1448.java) | |Medium|Tree, DFS|
1314
|1447|[Simplified Fractions](https://leetcode.com/problems/simplified-fractions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1447.java) | |Medium|Math|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.TreeMap;
6+
7+
public class _1451 {
8+
public static class Solution1 {
9+
public String arrangeWords(String text) {
10+
TreeMap<Integer, List<String>> map = new TreeMap<>();
11+
String[] words = text.split(" ");
12+
for (String word : words) {
13+
int len = word.length();
14+
if (!map.containsKey(len)) {
15+
map.put(len, new ArrayList<>());
16+
}
17+
map.get(len).add(word.toLowerCase());
18+
}
19+
StringBuilder sb = new StringBuilder();
20+
boolean first = true;
21+
for (int len : map.keySet()) {
22+
List<String> strings = map.get(len);
23+
for (String str : strings) {
24+
if (first) {
25+
str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
26+
first = false;
27+
}
28+
sb.append(str + " ");
29+
}
30+
}
31+
return sb.substring(0, sb.length() - 1);
32+
}
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1451;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1451Test {
10+
private static _1451.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1451.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("Is cool leetcode", solution1.arrangeWords("Leetcode is cool"));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)