VOOZH about

URL: https://www.geeksforgeeks.org/dsa/split-a-string-into-maximum-number-of-unique-substrings/

⇱ Split a string into maximum number of unique substrings - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Split a string into maximum number of unique substrings

Last Updated : 23 Jul, 2025

Given string str, the task is to split the string into the maximum number of unique substrings possible and print their count.

Examples: 

Input: str = "ababccc"
Output: 5
Explanation:
Split the given string into the substrings "a", "b", "ab", "c" and "cc".
Therefore, the maximum count of unique substrings is 5.

Input: str = "aba"
Output: 2

Approach: The problem can be solved by the Greedy approach. Follow the steps below to solve the problem: 

  1. Initialize a Set S.
  2. Iterate over the characters of the string str and for each i and find the substring up to that index.
  3. If the given substring is not present in the Set S, insert it updates the maximum count, and remove it from the Set, because the same character cannot be reused.
  4. Return the maximum count.

Below is the implementation of the above approach:


Output: 
5

 

Time Complexity: O()

Auxiliary Space: O(N)

Comment