Skip to content

Commit 8926917

Browse files
committed
day 7
1 parent 8d37516 commit 8926917

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Palindrome Partitioning II
3+
==========================
4+
5+
Given a string s, partition s such that every substring of the partition is a palindrome.
6+
7+
Return the minimum cuts needed for a palindrome partitioning of s.
8+
9+
Example 1:
10+
Input: s = "aab"
11+
Output: 1
12+
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
13+
14+
Example 2:
15+
Input: s = "a"
16+
Output: 0
17+
18+
Example 3:
19+
Input: s = "ab"
20+
Output: 1
21+
22+
Constraints:
23+
1 <= s.length <= 2000
24+
s consists of lower-case English letters only.
25+
*/
26+
27+
class Solution {
28+
public:
29+
vector<int> memo;
30+
31+
bool palendrome(string& s, int i, int j) {
32+
while(i < j) {
33+
if(s[i++] != s[j--]) return false;
34+
}
35+
return true;
36+
}
37+
38+
int dfs(int i, string& s) {
39+
if(i == s.size()) return 0;
40+
41+
if(memo[i] != -1) return memo[i];
42+
43+
int ans = s.size();
44+
for(int j = i; j < s.size(); ++j) {
45+
if(palendrome(s, i, j)) {
46+
ans = min(ans, 1 + dfs(j+1, s));
47+
}
48+
}
49+
50+
return memo[i] = ans;
51+
}
52+
53+
int minCut(string s) {
54+
memo = vector<int> (s.size()+1, -1);
55+
return dfs(0, s) - 1;
56+
}
57+
};

Leetcode Daily Challenge/August-2021/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
| 4. | [Path Sum II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3838/) | [cpp](./04.%20Path%20Sum%20II.cpp) |
99
| 5. | [Stone Game](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3870/) | [cpp](./05.%20Stone%20Game.cpp) |
1010
| 6. | [N-ary Tree Level Order Traversal](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3871/) | [cpp](./06.%20N-ary%20Tree%20Level%20Order%20Traversal.cpp) |
11+
| 7. | [Palindrome Partitioning II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3872/) | [cpp](./07.%20Palindrome%20Partitioning%20II.cpp) |

0 commit comments

Comments
 (0)