-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromePartitioning.cpp
More file actions
56 lines (50 loc) · 1.2 KB
/
palindromePartitioning.cpp
File metadata and controls
56 lines (50 loc) · 1.2 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
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
#include <string>
// https://leetcode.com/problems/palindrome-partitioning/
class Solution
{
public:
bool isPalindrome(std::string &str)
{
bool result = true;
int stringLength = str.length();
int start = 0, end = stringLength - 1;
while (result && start < end)
{
if (str[start] != str[end])
{
result = false;
}
start += 1;
end -= 1;
}
return result;
}
void getResult(std::string str, std::vector<std::vector<std::string>> &res, std::vector<std::string> &tempRes)
{
if (str.empty())
{
res.push_back(tempRes);
}
int strLength = str.size();
for (int i = 0; i < strLength; i += 1)
{
std::string leftStr = str.substr(0, i + 1);
if (isPalindrome(leftStr))
{
tempRes.push_back(leftStr);
std::string restStr = str.substr(i + 1);
getResult(restStr, res, tempRes);
tempRes.pop_back();
}
}
}
std::vector<std::vector<std::string>> partition(std::string s)
{
std::vector<std::vector<std::string>> result;
std::vector<std::string> tempResultVector;
getResult(s, result, tempResultVector);
return result;
}
};