Skip to main content

Split a String Into the Max Number of Unique Substrings

In this page, we will solve the Split a String Into the Max Number of Unique Substrings problem using different approaches. We will provide the implementation of the solution in JavaScript, TypeScript, Python, Java, C++, and more.

Problem Description​

Given a string s, return the maximum number of unique substrings that the given string can be split into.

Examples​

Example 1:

Input: s = "ababccc"
Output: 5
Explanation: One way to split s into the maximum number of unique substrings is ["a", "b", "ab", "c", "cc"].

Example 2:

Input: s = "aba"
Output: 2
Explanation: One way to split s into the maximum number of unique substrings is ["a", "ba"].

Constraints​

  • 1<=s.length<=161 <= s.length <= 16
  • s contains only lower case English letters.

Solution for Split a String Into the Max Number of Unique Substrings​

Intuition and Approach​

To solve this problem, we need to explore all possible ways to split the string into unique substrings. This can be effectively done using a backtracking approach. We will also consider a dynamic programming approach to enhance our solution.

Approach 1: Backtracking​

We use a backtracking approach to explore all possible splits. We maintain a set to keep track of unique substrings and try to split the string at every possible position recursively.

Implementation​

Live Editor
function splitStringIntoMaxUniqueSubstrings() {
  const s = "ababccc";

  const maxUniqueSplit = function(s) {
    const set = new Set();
    
    function backtrack(start) {
      if (start === s.length) return set.size;

      let maxCount = 0;
      for (let end = start + 1; end <= s.length; end++) {
        const substring = s.substring(start, end);
        if (!set.has(substring)) {
          set.add(substring);
          maxCount = Math.max(maxCount, backtrack(end));
          set.delete(substring);
        }
      }
      return maxCount;
    }
    
    return backtrack(0);
  };

  const result = maxUniqueSplit(s);
  return (
    <div>
      <p>
        <b>Input:</b> s = "{s}"
      </p>
      <p>
        <b>Output:</b> {result}
      </p>
    </div>
  );
}
Result
Loading...

Code in Different Languages​

Written by @manishh12
 function maxUniqueSplit(s) {
const set = new Set();

function backtrack(start) {
if (start === s.length) return set.size;

let maxCount = 0;
for (let end = start + 1; end <= s.length; end++) {
const substring = s.substring(start, end);
if (!set.has(substring)) {
set.add(substring);
maxCount = Math.max(maxCount, backtrack(end));
set.delete(substring);
}
}
return maxCount;
}

return backtrack(0);
}

Complexity Analysis​

  • Time Complexity: O(2n)O(2^n) due to the exponential number of ways to split the string.
  • Space Complexity: O(n)O(n) for the recursion stack and the set to store unique substrings.
tip

By using different approaches like backtracking and dynamic programming, we can solve the Split a String Into the Max Number of Unique Substrings problem efficiently. The choice of implementation language and approach depends on the specific requirements and constraints of the problem.

References​