Palindrome Partitioning

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"]
]


class Solution {
public:
vector<vector<string>> partition(string s)
{
vector<vector<string>> result;
vector<string> v;
for(int i=0;i<s.length();i++) v.push_back(" ");
generate(s,result,v,0,0);
return result;
}
bool check(const string& s,int l,int r)
{
while(l<r && s[l]==s[r])
{
l++;
r--;
}
if(l>=r)
return true;
else
return false;
}
void generate(string& s,vector<vector<string>>& result,vector<string>& v,int sdep,int vdep)
{
if(sdep==s.length())
{
vector<string> vnew;
for(int i=0;i<vdep;i++)
vnew.push_back(v[i]);
result.push_back(vnew);
return;
}
string news="";
for(int i=sdep;i<s.length();i++)
{
news=news+s[i];
if(check(s,sdep,i))
{
v[vdep]=news;
generate(s,result,v,i+1,vdep+1);
}
}
}
};

Palindrome Partitioning,布布扣,bubuko.com

时间: 2025-01-11 21:41:44

Palindrome Partitioning的相关文章

[LeetCode]132.Palindrome Partitioning II

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu

leetcode -- Palindrome Partitioning II

指责别人,看清自己 [问题描述] Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning

132. Palindrome Partitioning II (String; DP)

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",

[LeetCode][Java] Palindrome Partitioning

题目: 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",&

Palindrome Partitioning -- leetcode

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","

LeetCode: Palindrome Partitioning II 解题报告

Palindrome Partitioning II Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome pa

Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",

leetcode -- Palindrome Partitioning

谋事在人,成事在天 [问题描述] 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",&qu

LeetCode131:Palindrome Partitioning

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","