1715. Split A String Into The Max Number Of Unique Substrings¶
Difficulty: Medium
LeetCode Problem View on GitHub
1715. Split a String Into the Max Number of Unique Substrings
Medium
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc" Output: 5 Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba" Output: 2 Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa" Output: 1 Explanation: It is impossible to split the string any further.
Constraints:
-
1 <= s.length <= 16 -
scontains only lower case English letters.
Solution¶
class Solution {
public int maxUniqueSplit(String s) {
return solve(s, 0, new HashSet<>());
}
private int solve(String s, int left, HashSet<String> set) {
if (left >= s.length()) return 0;
int max = 0;
for (int right = left + 1; right <= s.length(); right++) {
String current = s.substring(left, right);
if (!set.contains(current)) {
set.add(current);
max = Math.max(max, 1 + solve(s, right, set));
set.remove(current);
}
}
return max;
}
static Debug dbg = new Debug();
static class Debug {
public static boolean LOCAL = true;
public static boolean getLocal() {
try {
return System.getProperty("LOCAL") == null;
}catch(SecurityException e) {
return false;
}
}
public static <T> String ts(T t) {
if(t==null) {
return "null";
}
if(t instanceof Iterable) {
return ts((Iterable<?>) t);
}else if(t instanceof int[]) {
String s = Arrays.toString((int[]) t);
return "{"+s.substring(1, s.length()-1)+"}";
}else if(t instanceof long[]) {
String s = Arrays.toString((long[]) t);
return "{"+s.substring(1, s.length()-1)+"}";
}else if(t instanceof char[]) {
String s = Arrays.toString((char[]) t);
return "{"+s.substring(1, s.length()-1)+"}";
}else if(t instanceof double[]) {
String s = Arrays.toString((double[]) t);
return "{"+s.substring(1, s.length()-1)+"}";
}else if(t instanceof boolean[]) {
String s = Arrays.toString((boolean[]) t);
return "{"+s.substring(1, s.length()-1)+"}";
}else if(t instanceof Object[]) {
return ts((Object[]) t);
}
return t.toString();
}
private static <T> String ts(T[] arr) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for(T t: arr) {
if(!first) ret.append(", ");
first = false;
ret.append(ts(t));
}
ret.append("}");
return ret.toString();
}
private static <T> String ts(Iterable<T> iter) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for(T t: iter) {
if(!first) ret.append(", ");
first = false;
ret.append(ts(t));
}
ret.append("}");
return ret.toString();
}
public static void print(Object... o) {
if(LOCAL) {
System.out.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": [");
for(int i = 0; i<o.length; i++) {
if(i!=0) System.out.print(", ");
System.out.print(ts(o[i]));
}
System.out.println("]");
}
}
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here