Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create potd- 21_10_2024.cpp #44

Merged
merged 1 commit into from
Oct 21, 2024
Merged
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
42 changes: 42 additions & 0 deletions october_2024/potd- 21_10_2024.cpp
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)
}
}
}
};
Loading