-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC1593.java
More file actions
46 lines (38 loc) · 1.17 KB
/
LC1593.java
File metadata and controls
46 lines (38 loc) · 1.17 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
45
46
import java.util.HashSet;
import java.util.Scanner;
public class LC1593 {
static int maxCount;
public static void backTrack(String s, HashSet<String> set, int index) {
int n = s.length();
// base case
if (index == s.length()) {
maxCount = Math.max(maxCount, set.size());
return;
}
// loop
for (int i = index; i < n; i++) {
// check if substring is present in set or not
String sub = s.substring(index, i + 1);
if (!set.contains(sub)) {
set.add(sub);
backTrack(s, set, i + 1);
set.remove(sub); // backtracking
}
}
}
public static int maxUniqueSplit(String s) {
maxCount = 0;
HashSet<String> set = new HashSet<>();
backTrack(s, set, 0);
return maxCount;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String Here : ");
String str = sc.nextLine();
System.out.println();
int ans = maxUniqueSplit(str);
System.out.println(ans);
sc.close();
}
}