-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromePartitioning.h
64 lines (52 loc) · 1.49 KB
/
PalindromePartitioning.h
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Author: naiyong, aonaiyong@gmail.com
Date: Oct 3, 2014
Problem: Palindrome Partitioning
Difficulty: 3
Source: https://oj.leetcode.com/problems/palindrome-partitioning/
Notes:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
]
Solution: Depth-first Search.
*/
#ifndef PALINDROMEPARTITIONING_H_
#define PALINDROMEPARTITIONING_H_
#include <vector>
using std::vector;
#include <string>
using std::string;
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> result;
vector<string> part;
partition(s, 0, part, result);
return result;
}
void partition(const string &s, int start, vector<string> &part, vector<vector<string>> &result) {
if (start == s.size()) {
result.push_back(part);
return;
}
for (int i = start; i < s.size(); ++i) {
string sub = s.substr(start, i - start + 1);
if (!isPalindrome(sub)) continue;
part.push_back(sub);
partition(s, i + 1, part, result);
part.pop_back();
}
}
bool isPalindrome(const string &s) {
for (int i = 0, j = s.size()-1; i < j; ++i, --j) {
if (s[i] != s[j]) return false;
}
return true;
}
};
#endif /* PALINDROMEPARTITIONING_H_ */