-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode131.cpp
More file actions
44 lines (44 loc) · 951 Bytes
/
LeetCode131.cpp
File metadata and controls
44 lines (44 loc) · 951 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
40
41
42
43
44
class Solution
{
public:
vector<vector<string>> result;
vector<string> part;
bool palindrome(string s, int l, int r)
{
while (l < r)
{
if (s[l] != s[r])
return false;
else
l++, r--;
}
return true;
}
void dfs(int index, string s)
{
if (index >= s.size())
{
result.push_back(part);
return;
}
for (int j = index; j < s.size(); j++)
{
if (palindrome(s, index, j))
{
string tmp = "";
for (int k = index; k <= j; k++)
{
tmp += s[k];
}
part.push_back(tmp);
dfs(j + 1, s);
part.pop_back();
}
}
}
vector<vector<string>> partition(string s)
{
dfs(0, s);
return result;
}
};