MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Solution

Class Solution

SplitAStringIntoTheMaxNumberOfUniqueSubstrings.java:1–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2 int maxCount;
3 public int maxUniqueSplit(String s) {
4 maxCount=0;
5 HashSet<String>set = new HashSet<>();
6 backtrack(s,set,0);
7 return maxCount;
8 }
9 // tc: n * 2^n
10 // sc: n substring
11 public void backtrack(String s, HashSet<String> set, int index){
12 //base case
13 int n = s.length();
14 if(index==n){
15 maxCount = Math.max(maxCount,set.size());
16 return;
17 }
18 //loop
19 for(int i=index;i<n;i++){
20 //check if substring is present in set or not
21 String sub = s.substring(index,i+1);
22 if(!set.contains(sub)){
23 set.add(sub);
24 backtrack(s,set,i+1);
25 set.remove(sub);
26 }
27 }
28 }
29
30}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected