← Home

1593. Split a String Into the Max Number of Unique Substrings

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
backtrackingC++Markdown
159

The trick here is to name the state correctly, then let the implementation follow. For 1593. Split a String Into the Max Number of Unique Substrings, the solution in this repository is mainly a backtracking solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: backtracking.

The notes already sitting in the source point us in the right direction:

  • backtracking

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are backtrack, maxUniqueSplit.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • Substring checks are convenient but not free, so they are part of the real complexity story.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//backtracking
02//Runtime: 520 ms, faster than 50.80% of C++ online submissions for Split a String Into the Max Number of Unique Substrings.
03//Memory Usage: 44.1 MB, less than 49.47% of C++ online submissions for Split a String Into the Max Number of Unique Substrings.
04class Solution {
05public:
06    void backtrack(int cur, string& s, 
07       unordered_set<string>& split, int& ans){
08        if(cur == s.size()){
09            ans = max(ans, (int)split.size());
10        }else{
11            for(int l = 1; l <= s.size() - cur; ++l){
12                if(split.find(s.substr(cur, l)) == split.end()){
13                    split.insert(s.substr(cur, l));
14                    backtrack(cur+l, s, split, ans);
15                    split.erase(s.substr(cur, l));
16                }
17            }
18        }
19    }
20    
21    int maxUniqueSplit(string s) {
22        unordered_set<string> split;
23        int ans = 0;
24        
25        backtrack(0, s, split, ans);
26        
27        return ans;
28    }
29};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.