|
| 1 | +--- |
| 2 | + |
| 3 | +id: split-a-string-into-the-max-number-of-unique-substrings |
| 4 | +title: Split a String Into the Max Number of Unique Substrings |
| 5 | +sidebar_label: 1593-Split-a-String-Into-the-Max-Number-of-Unique-Substrings |
| 6 | +tags: |
| 7 | + - String |
| 8 | + - Backtracking |
| 9 | + - Dynamic Programming |
| 10 | + - LeetCode |
| 11 | + - Python |
| 12 | + - JavaScript |
| 13 | + - TypeScript |
| 14 | + - Java |
| 15 | + - C++ |
| 16 | +description: "This is a solution to the Split a String Into the Max Number of Unique Substrings problem on LeetCode." |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +## Problem Description |
| 23 | + |
| 24 | +Given a string `s`, return the maximum number of unique substrings that the given string can be split into. |
| 25 | + |
| 26 | +### Examples |
| 27 | + |
| 28 | +**Example 1:** |
| 29 | + |
| 30 | +```plaintext |
| 31 | +Input: s = "ababccc" |
| 32 | +Output: 5 |
| 33 | +Explanation: One way to split s into the maximum number of unique substrings is ["a", "b", "ab", "c", "cc"]. |
| 34 | +``` |
| 35 | + |
| 36 | +**Example 2:** |
| 37 | + |
| 38 | +```plaintext |
| 39 | +Input: s = "aba" |
| 40 | +Output: 2 |
| 41 | +Explanation: One way to split s into the maximum number of unique substrings is ["a", "ba"]. |
| 42 | +``` |
| 43 | + |
| 44 | +### Constraints |
| 45 | + |
| 46 | +- $1 <= s.length <= 16$ |
| 47 | +- `s` contains only lower case English letters. |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Solution for Split a String Into the Max Number of Unique Substrings |
| 52 | + |
| 53 | +### Intuition and Approach |
| 54 | + |
| 55 | +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. |
| 56 | + |
| 57 | +<Tabs> |
| 58 | + <tabItem value="Backtracking" label="Backtracking"> |
| 59 | + |
| 60 | +### Approach 1: Backtracking |
| 61 | + |
| 62 | +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. |
| 63 | + |
| 64 | +#### Implementation |
| 65 | + |
| 66 | +```jsx live |
| 67 | +function splitStringIntoMaxUniqueSubstrings() { |
| 68 | + const s = "ababccc"; |
| 69 | + |
| 70 | + const maxUniqueSplit = function(s) { |
| 71 | + const set = new Set(); |
| 72 | + |
| 73 | + function backtrack(start) { |
| 74 | + if (start === s.length) return set.size; |
| 75 | + |
| 76 | + let maxCount = 0; |
| 77 | + for (let end = start + 1; end <= s.length; end++) { |
| 78 | + const substring = s.substring(start, end); |
| 79 | + if (!set.has(substring)) { |
| 80 | + set.add(substring); |
| 81 | + maxCount = Math.max(maxCount, backtrack(end)); |
| 82 | + set.delete(substring); |
| 83 | + } |
| 84 | + } |
| 85 | + return maxCount; |
| 86 | + } |
| 87 | + |
| 88 | + return backtrack(0); |
| 89 | + }; |
| 90 | + |
| 91 | + const result = maxUniqueSplit(s); |
| 92 | + return ( |
| 93 | + <div> |
| 94 | + <p> |
| 95 | + <b>Input:</b> s = "{s}" |
| 96 | + </p> |
| 97 | + <p> |
| 98 | + <b>Output:</b> {result} |
| 99 | + </p> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +#### Code in Different Languages |
| 106 | + |
| 107 | +<Tabs> |
| 108 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 109 | + <SolutionAuthor name="@manishh12"/> |
| 110 | + ```javascript |
| 111 | + function maxUniqueSplit(s) { |
| 112 | + const set = new Set(); |
| 113 | + |
| 114 | + function backtrack(start) { |
| 115 | + if (start === s.length) return set.size; |
| 116 | + |
| 117 | + let maxCount = 0; |
| 118 | + for (let end = start + 1; end <= s.length; end++) { |
| 119 | + const substring = s.substring(start, end); |
| 120 | + if (!set.has(substring)) { |
| 121 | + set.add(substring); |
| 122 | + maxCount = Math.max(maxCount, backtrack(end)); |
| 123 | + set.delete(substring); |
| 124 | + } |
| 125 | + } |
| 126 | + return maxCount; |
| 127 | + } |
| 128 | + |
| 129 | + return backtrack(0); |
| 130 | + } |
| 131 | + ``` |
| 132 | + |
| 133 | + </TabItem> |
| 134 | + <TabItem value="TypeScript" label="TypeScript"> |
| 135 | + <SolutionAuthor name="@manishh12"/> |
| 136 | + ```typescript |
| 137 | + function maxUniqueSplit(s: string): number { |
| 138 | + const set = new Set<string>(); |
| 139 | + |
| 140 | + function backtrack(start: number): number { |
| 141 | + if (start === s.length) return set.size; |
| 142 | +
|
| 143 | + let maxCount = 0; |
| 144 | + for (let end = start + 1; end <= s.length; end++) { |
| 145 | + const substring = s.substring(start, end); |
| 146 | + if (!set.has(substring)) { |
| 147 | + set.add(substring); |
| 148 | + maxCount = Math.max(maxCount, backtrack(end)); |
| 149 | + set.delete(substring); |
| 150 | + } |
| 151 | + } |
| 152 | + return maxCount; |
| 153 | + } |
| 154 | + |
| 155 | + return backtrack(0); |
| 156 | + } |
| 157 | + ``` |
| 158 | + |
| 159 | + </TabItem> |
| 160 | + <TabItem value="Python" label="Python"> |
| 161 | + <SolutionAuthor name="@manishh12"/> |
| 162 | + ```python |
| 163 | + class Solution: |
| 164 | + def maxUniqueSplit(self, s: str) -> int: |
| 165 | + def backtrack(start, seen): |
| 166 | + if start == len(s): |
| 167 | + return len(seen) |
| 168 | + |
| 169 | + max_count = 0 |
| 170 | + for end in range(start + 1, len(s) + 1): |
| 171 | + substring = s[start:end] |
| 172 | + if substring not in seen: |
| 173 | + seen.add(substring) |
| 174 | + max_count = max(max_count, backtrack(end, seen)) |
| 175 | + seen.remove(substring) |
| 176 | + return max_count |
| 177 | + |
| 178 | + return backtrack(0, set()) |
| 179 | + ``` |
| 180 | + |
| 181 | + </TabItem> |
| 182 | + <TabItem value="Java" label="Java"> |
| 183 | + <SolutionAuthor name="@manishh12"/> |
| 184 | + ```java |
| 185 | + import java.util.HashSet; |
| 186 | + import java.util.Set; |
| 187 | +
|
| 188 | + class Solution { |
| 189 | + public int maxUniqueSplit(String s) { |
| 190 | + return backtrack(s, 0, new HashSet<>()); |
| 191 | + } |
| 192 | +
|
| 193 | + private int backtrack(String s, int start, Set<String> seen) { |
| 194 | + if (start == s.length()) return seen.size(); |
| 195 | + |
| 196 | + int maxCount = 0; |
| 197 | + for (int end = start + 1; end <= s.length(); end++) { |
| 198 | + String substring = s.substring(start, end); |
| 199 | + if (!seen.contains(substring)) { |
| 200 | + seen.add(substring); |
| 201 | + maxCount = Math.max(maxCount, backtrack(s, end, seen)); |
| 202 | + seen.remove(substring); |
| 203 | + } |
| 204 | + } |
| 205 | + return maxCount; |
| 206 | + } |
| 207 | + } |
| 208 | + ``` |
| 209 | + |
| 210 | + </TabItem> |
| 211 | + <TabItem value="C++" label="C++"> |
| 212 | + <SolutionAuthor name="@manishh12"/> |
| 213 | + ```cpp |
| 214 | + #include <unordered_set> |
| 215 | + #include <string> |
| 216 | +
|
| 217 | + class Solution { |
| 218 | + public: |
| 219 | + int maxUniqueSplit(std::string s) { |
| 220 | + return backtrack(s, 0, std::unordered_set<std::string>()); |
| 221 | + } |
| 222 | +
|
| 223 | + private: |
| 224 | + int backtrack(const std::string& s, int start, std::unordered_set<std::string> seen) { |
| 225 | + if (start == s.size()) return seen.size(); |
| 226 | + |
| 227 | + int maxCount = 0; |
| 228 | + for (int end = start + 1; end <= s.size(); ++end) { |
| 229 | + std::string substring = s.substr(start, end - start); |
| 230 | + if (seen.find(substring) == seen.end()) { |
| 231 | + seen.insert(substring); |
| 232 | + maxCount = std::max(maxCount, backtrack(s, end, seen)); |
| 233 | + seen.erase(substring); |
| 234 | + } |
| 235 | + } |
| 236 | + return maxCount; |
| 237 | + } |
| 238 | + }; |
| 239 | + ``` |
| 240 | + |
| 241 | + </TabItem> |
| 242 | +</Tabs> |
| 243 | + |
| 244 | +#### Complexity Analysis |
| 245 | + |
| 246 | +- Time Complexity: $$O(2^n)$$ due to the exponential number of ways to split the string. |
| 247 | +- Space Complexity: $$O(n)$$ for the recursion stack and the set to store unique substrings. |
| 248 | + |
| 249 | +</tabItem> |
| 250 | + |
| 251 | +</Tabs> |
| 252 | + |
| 253 | +:::tip |
| 254 | + |
| 255 | +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. |
| 256 | + |
| 257 | +::: |
| 258 | + |
| 259 | +## References |
| 260 | + |
| 261 | +- **LeetCode Problem:** [Split a String Into the Max Number of Unique Substrings](https://leetcode.com/problems/split-a-string-into-the-max-number-of-unique-substrings/) |
| 262 | +- **Solution Link:** [Split a String Into the Max Number of Unique Substrings Solution on LeetCode](https://leetcode.com/problems/split-a-string-into-the-max-number-of-unique-substrings/solution/) |
| 263 | +- **Authors LeetCode Profile:** [Manish Kumar Gupta](https://leetcode.com/_manishh12/) |
| 264 | + |
0 commit comments