Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions 1593. Split a String Into the Max Number of Unique Substrings
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
1593. Split a String Into the Max Number of Unique Substrings
Given a string s, return the maximum number of unique substrings that the given string can be split into.

You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.

A substring is a contiguous sequence of characters within a string.

Example 1:

Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:

Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:

Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.

Constraints:

1 <= s.length <= 16

s contains only lower case English letters.

Solution:

class UniqueSubstringFinder {
public:
// Constructor
UniqueSubstringFinder(const std::string& input) : str(input) {}

// Function to find all unique substrings
std::set<std::string> getUniqueSubstrings() {
std::set<std::string> uniqueSubstrings;
int n = str.length();

// Iterate over all possible lengths of substrings
for (int length = 1; length <= n; ++length) {
// Iterate over all starting points for substrings of the given length
for (int start = 0; start <= n - length; ++start) {
std::string substring = str.substr(start, length);
uniqueSubstrings.insert(substring); // Insert into set (ensures uniqueness)
}
}

return uniqueSubstrings;
}

private:
std::string str; // Input string
};