-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromePartitioning.cpp
More file actions
39 lines (36 loc) · 980 Bytes
/
palindromePartitioning.cpp
File metadata and controls
39 lines (36 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Source: https://leetcode.com/problems/palindrome-partitioning/
// Author: Miao Zhang
// Date: 2021-01-20
class Solution {
public:
bool isPalindrome(string s) {
if (s.size() == 0) return true;
int l = 0;
int r = s.size() - 1;
while (l <= r) {
if (s[l] != s[r]) return false;
l++;
r--;
}
return true;
}
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
dfs(s, res, {});
return res;
}
void dfs(string s, vector<vector<string>>& res, vector<string> path) {
if (s.empty()) {
res.push_back(path);
return;
}
for (int i = 1; i <= s.size(); i++) {
string pre = s.substr(0, i);
if (isPalindrome(pre)) {
path.push_back(pre);
dfs(s.substr(i), res, path);
path.pop_back();
}
}
}
};