Skip to content

Commit 2589a65

Browse files
committedApr 30, 2021
pal patition
1 parent 2c78d72 commit 2589a65

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Palindrome Partitioning
3+
=======================
4+
5+
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
6+
7+
A palindrome string is a string that reads the same backward as forward.
8+
9+
Example 1:
10+
Input: s = "aab"
11+
Output: [["a","a","b"],["aa","b"]]
12+
13+
Example 2:
14+
Input: s = "a"
15+
Output: [["a"]]
16+
17+
Constraints:
18+
1 <= s.length <= 16
19+
s contains only lowercase English letters.
20+
*/
21+
22+
class Solution
23+
{
24+
public:
25+
bool check(string &s, int i, int j)
26+
{
27+
while (i < j)
28+
{
29+
if (s[i++] != s[j--])
30+
return false;
31+
}
32+
return true;
33+
}
34+
35+
void dfs(string &s, int idx, vector<vector<string>> &ans, vector<string> atn)
36+
{
37+
if (s.size() == idx)
38+
{
39+
ans.push_back(atn);
40+
return;
41+
}
42+
43+
for (int i = idx; i < s.size(); ++i)
44+
{
45+
if (check(s, idx, i))
46+
{
47+
atn.push_back(s.substr(idx, i - idx + 1));
48+
dfs(s, i + 1, ans, atn);
49+
atn.pop_back();
50+
}
51+
}
52+
}
53+
54+
vector<vector<string>> partition(string s)
55+
{
56+
vector<vector<string>> ans;
57+
dfs(s, 0, ans, {});
58+
return ans;
59+
}
60+
};

‎Striver Sheet/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
- [Subsets II](https://leetcode.com/problems/subsets-ii/) - [Cpp Soultion](./Day-9/Subsets%20II.cpp)
8080
- [Combination Sum](https://leetcode.com/problems/combination-sum/) - [Cpp Soultion](./Day-9/Combination%20Sum.cpp)
8181
- [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) - [Cpp Soultion](./Day-9/Combination%20Sum%20II.cpp)
82-
- []() - [Cpp Soultion](./Day-9/.cpp)
82+
- [Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) - [Cpp Soultion](./Day-9/Palindrome%20Partitioning.cpp)
8383
- []() - [Cpp Soultion](./Day-9/.cpp)
8484

8585
### Day 10 (Recursion, Backtrack)

0 commit comments

Comments
 (0)
Please sign in to comment.