Skip to content
Closed
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
33 changes: 33 additions & 0 deletions cpp/131-Palindrome-Partitioning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
bool isPali(const string& s, int l, int r) {
while (l < r) {
if (s[l] != s[r])
return false;
++l, --r;
}
return true;
}

void solve(const string& s, vector<vector<string>>& res, vector<string>& part, int idx) {
if (idx >= s.size()) {
res.push_back(part);
return;
}
for (int j = idx; j < s.size(); ++j) {
if (isPali(s, idx, j)) {
part.push_back(s.substr(idx, j - idx + 1));
solve(s, res, part, j + 1);
part.pop_back();
}
}
}

vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> part;

solve(s, res, part, 0);
return res;
}
};