leetcode: Minimum Window Substring
2014-12-07 00:31
351 查看
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"]
]
Show Tags
Have you met this question in a real interview?
Yes
No
Discuss
class Solution {
public:
bool isPalindrome(string s)
{
if (s.size() == 1)
return true;
int front = 0;
int rear = s.size()-1;
while (front < rear)
{
if (s.at(front) == s.at(rear))
{
front++;
rear--;
}
else
{
return false;
}
}
return true;
}
void partitionInternal(string s, vector<string> &curVector, vector<vector<string>> &retVector)
{
if (s.size() == 0)
{
retVector.push_back(curVector);
}
for (int i=1; i<=s.size(); i++)
{
string substring = s.substr(0, i);
if (isPalindrome(substring))
{
curVector.push_back(substring);
partitionInternal(s.substr(i, s.size()-i), curVector, retVector);
curVector.pop_back();
}
// we can not just return if isPalindrome(substring) is false
// for example abbab; ba is false, but bab is ture
}
}
vector<vector<string>> partition(string s) {
vector<vector<string>> retVector;
for (int i=1; i<=s.size(); i++)
{
string substring = s.substr(0, i);
vector<string> curVector;
if (isPalindrome(substring))
{
curVector.push_back(substring);
partitionInternal(s.substr(i, s.size()-i), curVector, retVector);
<span style="white-space:pre"> </span>curVector.pop_back(); // this pop_back is important
}
}
return retVector;
}
};
相关文章推荐
- LeetCode – Refresh – Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- [Leetcode]Minimum Window Substring
- leetcode minimum window substring
- 【LeetCode】Minimum Window Substring
- Leetcode: Minimum Window Substring
- Leetcode: Minimum Window Substring
- 【leetcode】Minimum Window Substring
- LeetCode - Minimum Window Substring
- Minimum Window Substring 最小子串窗口@LeetCode
- LeetCode-Minimum Window Substring -- 窗口问题
- LeetCode: Minimum Window Substring
- leetcode——Minimum Window Substring
- LeetCode: 76. Minimum Window Substring
- Leetcode231: Minimum Window Substring
- [Leetcode] minimum window substring 最小字符窗口
- LeetCode76 Minimum Window Substring(两种解法)
- leetcode76题 Minimum Window Substring
- leetcode解题笔记-Minimum Window Substring
- 【LeetCode】Minimum Window Substring