Program to find split a string into the max number of unique substrings in Python

Given a string s, we need to find the maximum number of unique substrings that the string can be split into. Each substring must be non-empty, and when concatenated together, they should form the original string. The key constraint is that all substrings must be different from each other.

For example, if we have s = "pqpqrrr", we can split it as ['p', 'q', 'pq', 'r', 'rr'] to get 5 unique substrings. However, splitting it as ['p', 'q', 'p', 'q', 'r', 'rr'] is invalid because 'p' and 'q' appear multiple times.

Algorithm

We use backtracking with a depth-first search (DFS) approach ?

  • Use a result variable to track the maximum count
  • For each position in the string, try all possible substrings starting from that position
  • If a substring is not already used, add it to the current path and recursively process the remaining string
  • When we reach the end of the string, update the maximum count

Implementation

def solve(s):
    res = [0]
    
    def dfs(s, path=set()):
        if not s:
            res[0] = max(res[0], len(path))
            return
        
        for i in range(1, len(s) + 1):
            x = s[:i]
            if x not in path:
                dfs(s[i:], path | {x})
    
    dfs(s)
    return res[0]

# Test the function
s = "pqpqrrr"
result = solve(s)
print(f"Maximum unique substrings for '{s}': {result}")
Maximum unique substrings for 'pqpqrrr': 5

How It Works

The algorithm explores all possible ways to split the string ?

  • Base case: When the string is empty, we've found a complete split and update the maximum count
  • Recursive case: For each possible substring starting from the beginning, if it's unique, we add it to our current path and recursively process the remaining string
  • Backtracking: The set operations ensure we don't reuse substrings in the same path

Step-by-Step Example

For string "pqpqrrr", one optimal split is ?

s = "pqpqrrr"

# One possible optimal split:
split_example = ['p', 'q', 'pq', 'r', 'rr']
print("Split:", split_example)
print("Count:", len(split_example))
print("Concatenated:", ''.join(split_example))
print("All unique?", len(split_example) == len(set(split_example)))
Split: ['p', 'q', 'pq', 'r', 'rr']
Count: 5
Concatenated: pqpqrrr
All unique? True

Time Complexity

The time complexity is O(2^n) in the worst case, where n is the length of the string, as we explore all possible ways to split the string. The space complexity is O(n) for the recursion stack and the set storing current substrings.

Conclusion

This backtracking solution efficiently finds the maximum number of unique substrings by exploring all possible splits. The key insight is using a set to track used substrings and recursively trying all possible substring combinations.

---
Updated on: 2026-03-26T13:49:10+05:30

457 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements