-
Notifications
You must be signed in to change notification settings - Fork 481
/
0131.cpp
41 lines (40 loc) · 967 Bytes
/
0131.cpp
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
40
41
class Solution
{
public:
vector<vector<string>> partition(string s)
{
vector<vector<string>> pars;
vector<string> par;
partition(s, 0, par, pars);
return pars;
}
private:
void partition(string& s, int start, vector<string>& par, vector<vector<string>>& pars)
{
int n = s.length();
if (start == n)
{
pars.push_back(par);
}
else
{
for (int i = start; i < n; i++)
{
if (isPalindrome(s, start, i))
{
par.push_back(s.substr(start, i - start + 1));
partition(s, i + 1, par, pars);
par.pop_back();
}
}
}
}
bool isPalindrome(string& s, int l, int r)
{
while (l < r)
{
if (s[l++] != s[r--]) return false;
}
return true;
}
};