8000 add 1592 · githubth/Leetcode-1@b740f89 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b740f89

Browse files
add 1592
1 parent 53e47de commit b740f89

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-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+
|1592|[Rearrange Spaces Between Words](https://leetcode.com/problems/rearrange-spaces-between-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1592.java) ||String|Easy|
1112
|1583|[Count Unhappy Friends](https://leetcode.com/problems/count-unhappy-friends/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1583.java) ||Array|Medium|
1213
|1582|[Special Positions in a Binary Matrix](https://leetcode.com/problems/special-positions-in-a-binary-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1582.java) ||Array|Easy|
1314
|1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1576.java) |[:tv:](https://youtu.be/SJBDLYqrIsM)|String|Easy|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1592 {
4+
public static class Solution1 {
5+
public String reorderSpaces(String text) {
6+
int spaceCount = 0;
7+
for (char c : text.toCharArray()) {
8+
if (c == ' ') {
9+
spaceCount++;
10+
}
11+
}
12+
String[] words = text.trim().split("\\s+");
13+
if (words.length == 1) {
14+
StringBuilder sb = new StringBuilder(words[0]);
15+
for (int i = 0; i < spaceCount; i++) {
16+
sb.append(" ");
17+
}
18+
return sb.toString();
19+
}
20+
int trailingSpaces = spaceCount % (words.length - 1);
21+
int newSpaces = spaceCount / (words.length - 1);
22+
StringBuilder sb = new StringBuilder();
23+
for (int j = 0; j < words.length; j++) {
24+
sb.append(words[j]);
25+
if (j < words.length - 1) {
26+
for (int i = 0; i < newSpaces; i++) {
27+
sb.append(" ");
28+
}
29+
} else {
30+
for (int i = 0; i < trailingSpaces; i++) {
31+
sb.append(" ");
32+
}
33+
}
34+
}
35+
return sb.toString();
36+
}
37+
}
38+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1592;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1592Test {
10+
private static _1592.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1592.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("hello ", solution1.reorderSpaces(" hello"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("this is a sentence", solution1.reorderSpaces(" this is a sentence "));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("practice makes perfect ", solution1.reorderSpaces(" practice makes perfect"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("hello world", solution1.reorderSpaces("hello world"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals("walks udp package into bar a ", solution1.reorderSpaces(" walks udp package into bar a"));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals("a", solution1.reorderSpaces("a"));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)
0