-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplitStringIntoTheMaxNumberOfUniqueSubstrings.java
52 lines (47 loc) · 1.6 KB
/
SplitStringIntoTheMaxNumberOfUniqueSubstrings.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.ArrayList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Zawad Zamil
* Date: 10/21/24
* Time: 12:19 PM
* Email: zawad@zaagsys.com
*/
public class SplitStringIntoTheMaxNumberOfUniqueSubstrings {
public int maxUniqueSplit(String s) {
List<String> list = new ArrayList<>();
StringBuffer sb = new StringBuffer();
for (char c : s.toCharArray()) {
if (!list.contains(c + "")) {
list.add(sb.toString() + c);
sb = new StringBuffer();
} else {
sb.append(c);
if (!list.contains(sb.toString())) {
list.add(sb.toString());
sb = new StringBuffer();
}
}
}
List<String> secondList = new ArrayList<>();
for (char c : s.toCharArray()) {
if (!secondList.contains(c + "")) {
secondList.add( c + "");
} else {
String last = secondList.getLast();
secondList.removeLast();
secondList.add(last + c);
if (!secondList.contains(sb.toString())) {
secondList.add(sb.toString());
sb = new StringBuffer();
}
}
}
System.out.println(secondList);
return list.size();
}
public static void main(String[] args) {
SplitStringIntoTheMaxNumberOfUniqueSubstrings substrings = new SplitStringIntoTheMaxNumberOfUniqueSubstrings();
System.out.println(substrings.maxUniqueSplit("wwwzfvedwfvhsww"));
}
}