-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from vidhirohira/patch-2
Create potd- 21_10_2024.cpp
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class Solution { | ||
public: | ||
// Main function to find the maximum number of unique substrings | ||
int maxUniqueSplit(string s) { | ||
unordered_set<string> seen; // Set to store unique substrings | ||
int maxCount = 0; // Variable to store the maximum count of unique substrings | ||
|
||
// Call the backtracking function to find the maximum count | ||
backtrack(s, 0, seen, 0, maxCount); | ||
|
||
return maxCount; // Return the final result | ||
} | ||
|
||
private: | ||
// Backtracking function to explore all possible splits | ||
void backtrack(const string& s, int start, unordered_set<string>& seen, | ||
int count, int& maxCount) { | ||
// Pruning: If the remaining characters plus current count can't exceed maxCount, return | ||
if (count + (s.size() - start) <= maxCount) return; | ||
|
||
// Base case: If we've reached the end of the string | ||
if (start == s.size()) { | ||
maxCount = max(maxCount, count); // Update maxCount if necessary | ||
return; | ||
} | ||
|
||
// Try all possible substrings starting from 'start' | ||
for (int end = start + 1; end <= s.size(); ++end) { | ||
string substring = s.substr(start, end - start); // Extract substring | ||
|
||
// If the substring is not in the set (i.e., it's unique) | ||
if (seen.find(substring) == seen.end()) { | ||
seen.insert(substring); // Add the substring to the set | ||
|
||
// Recursively call backtrack with updated parameters | ||
backtrack(s, end, seen, count + 1, maxCount); | ||
|
||
seen.erase(substring); // Remove the substring from the set (backtracking) | ||
} | ||
} | ||
} | ||
}; |