-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfGoodWaysToSplitAString.java
More file actions
44 lines (39 loc) · 1.3 KB
/
NumberOfGoodWaysToSplitAString.java
File metadata and controls
44 lines (39 loc) · 1.3 KB
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
35
36
37
38
39
40
41
42
43
44
import java.util.HashSet;
import java.util.Set;
import java.util.stream.IntStream;
/*
* https://leetcode.com/problems/number-of-good-ways-to-split-a-string/
*/
public class NumberOfGoodWaysToSplitAString {
public int numSplits(String s) {
Set<Character> setLeft = new HashSet<>();
Set<Character> setRight = new HashSet<>();
int[] left = new int[s.length()];
int[] right = new int[s.length()];
int uniqueCharacters = 0;
int length = s.toCharArray().length;
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if (!setLeft.contains(c)) {
setLeft.add(c);
uniqueCharacters++;
}
left[i] = uniqueCharacters;
}
uniqueCharacters = 0;
for (int i = length - 1; i >= 0; i--) {
char c = s.charAt(i);
if (!setRight.contains(c)) {
setRight.add(c);
uniqueCharacters++;
}
right[i] = uniqueCharacters;
}
return (int) IntStream.range(0, length - 1)
.filter(i -> left[i] == right[i + 1])
.count();
}
public static void main(String[] args) {
System.out.println(new NumberOfGoodWaysToSplitAString().numSplits("aacaba")); // 2
}
}