-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC0131.cpp
More file actions
executable file
·37 lines (32 loc) · 779 Bytes
/
LC0131.cpp
File metadata and controls
executable file
·37 lines (32 loc) · 779 Bytes
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
/*
Problem Statement: https://leetcode.com/problems/palindrome-partitioning/
Time: O(n • 2ⁿ⁻¹)
Space: O(n • 2ⁿ⁻¹)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
vector<vector<string>> partition(string s) {
int n = s.length();
vector<vector<string>> dp[n + 1];
// initialization
dp[0] = { {} };
// dynamic programming
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++) {
if (!is_palindrome(i - j, i - 1, s))
continue;
for (vector<string>& x: dp[i - j]) {
dp[i].push_back(x);
dp[i].back().push_back(s.substr(i - j, j));
}
}
return dp[n];
}
bool is_palindrome(int l, int r, string& s) {
while (l < r)
if (s[l++] != s[r--])
return false;
return true;
}
};