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

Sign up
Appearance settings

Commit 702fac7

Browse files
solves #2788: Split Strings by Separator in java
1 parent ac81931 commit 702fac7

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

‎README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@
840840
| 2769 | [Find the Maximum Achievable Number](https://leetcode.com/problems/find-the-maximum-achievable-number) | [![Java](assets/java.png)](src/FindTheMaximumAchievableNumber.java) | |
841841
| 2778 | [Sum of Squares of Special Elements](https://leetcode.com/problems/sum-of-squares-of-special-elements) | [![Java](assets/java.png)](src/SumOfSquaresOfSpecialElements.java) | |
842842
| 2784 | [Check if Array is Good](https://leetcode.com/problems/check-if-array-is-good) | [![Java](assets/java.png)](src/CheckIfArrayIsGood.java) | |
843-
| 2788 | [Split Strings by Separator](https://leetcode.com/problems/split-strings-by-separator) | | |
843+
| 2788 | [Split Strings by Separator](https://leetcode.com/problems/split-strings-by-separator) | [![Java](assets/java.png)](src/SplitStringsBySeparator.java) | |
844844
| 2798 | [Number of Employees Who Met the Target](https://leetcode.com/problems/number-of-employees-who-met-the-target) | | |
845845
| 2806 | [Account Balance After Rounded Purchase](https://leetcode.com/problems/account-balance-after-rounded-purchase) | | |
846846
| 2810 | [Faulty Keyboard](https://leetcode.com/problems/faulty-keyboard) | | |

‎src/HelloWorld.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import java.util.PriorityQueue;
2-
import java.util.Queue;
1+
import java.util.*;
32

43
public class HelloWorld {
54
public static void main(String[] args) {
6-
final Queue<Integer> queue = new PriorityQueue<>();
7-
queue.add(10);
8-
queue.add(4);
9-
queue.add(-2);
5+
final List<String> words = new ArrayList<>();
6+
words.add("apple.ball.cat");
7+
words.add("apple.ball.cat.dog");
108

11-
System.out.println(queue.peek());
9+
final String word = "apple.ball.cat.dog";
10+
11+
System.out.println(Arrays.toString(word.split("/.")));
1212
}
1313
}

‎src/SplitStringsBySeparator.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// https://leetcode.com/problems/split-strings-by-separator
2+
// W: number of words
3+
// s: string in words
4+
// T: O(W * |s|)
5+
// S: O(W * |s|)
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public class SplitStringsBySeparator {
11+
public List<String> splitWordsBySeparator(List<String> words, char separator) {
12+
final List<String> result = new ArrayList<>();
13+
for (String word : words) {
14+
final List<String> splitStrings = split(word, separator);
15+
for (String split : splitStrings) {
16+
if (!split.isEmpty()) {
17+
result.add(split);
18+
}
19+
}
20+
}
21+
return result;
22+
}
23+
24+
private List<String> split(String string, char separator) {
25+
final List<String> result = new ArrayList<>();
26+
StringBuilder builder = new StringBuilder();
27+
for (int i = 0 ; i < string.length() ; i++) {
28+
if (string.charAt(i) == separator) {
29+
result.add(builder.toString());
30+
builder = new StringBuilder();
31+
} else {
32+
builder.append(string.charAt(i));
33+
}
34+
}
35+
if (!builder.isEmpty()) {
36+
result.add(builder.toString());
37+
}
38+
return result;
39+
}
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /