
g0101_0200.s0131_palindrome_partitioning.Solution.cpp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
// #Backtracking #Big_O_Time_O(N*2^N)_Space_O(2^N*N)
// #2024_05_27_Time_69_ms_(83.20%)_Space_52.8_MB_(63.51%)
#include
#include
class Solution {
public:
std::vector> partition(std::string s) {
std::vector> res;
std::vector currArr;
backtracking(res, currArr, s, 0);
return res;
}
private:
void backtracking(std::vector>& res, std::vector& currArr, const std::string& s, int start) {
if (start == s.length()) {
res.push_back(currArr);
}
for (int end = start; end < s.length(); end++) {
if (!isPalindrome(s, start, end)) {
continue;
}
currArr.push_back(s.substr(start, end - start + 1));
backtracking(res, currArr, s, end + 1);
currArr.pop_back();
}
}
bool isPalindrome(const std::string& s, int start, int end) {
while (start < end && s[start] == s[end]) {
start++;
end--;
}
return start >= end;
}
};
© 2015 - 2025 Weber Informatics LLC | Privacy Policy