forked from mahendrarathore1742/leetcode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode0131-palindrome-partitioning.cpp
More file actions
47 lines (42 loc) · 1.08 KB
/
Copy pathleetcode0131-palindrome-partitioning.cpp
File metadata and controls
47 lines (42 loc) · 1.08 KB
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
45
46
47
/*
* Copyright (C) 2018 all rights reserved.
*
* Author: Houmin Wei <houmin.wei@outlook.com>
*
* Source: https://leetcode.com/problems/palindrome-partitioning
*/
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<string> path;
vector<vector<string>> ret;
dfs(s, 0, path, ret);
return ret;
}
void dfs(string& s, int begin, vector<string>& path, vector<vector<string>>& ret) {
int n = s.length();
if (begin == n) {
ret.push_back(path);
return;
}
for (int i = begin; i < n; i++) {
string key = s.substr(begin, i-begin+1);
if (isValid(key)) {
path.push_back(key);
dfs(s, i+1, path, ret);
path.pop_back();
}
}
}
bool isValid(string& s) {
int n = s.length();
for (int i = 0, j = n-1; i <= j; i++, j--) {
if (s[i] != s[j])
return false;
}
return true;
}
};