-
Preface
- FAQ
-
Part I - Basics
- Basics Data Structure
- Basics Sorting
- Basics Algorithm
- Basics Misc
-
Part II - Coding
- String
-
Integer Array
-
Remove Element
-
Zero Sum Subarray
-
Subarray Sum K
-
Subarray Sum Closest
-
Recover Rotated Sorted Array
-
Product of Array Exclude Itself
-
Partition Array
-
First Missing Positive
-
2 Sum
-
3 Sum
-
3 Sum Closest
-
Remove Duplicates from Sorted Array
-
Remove Duplicates from Sorted Array II
-
Merge Sorted Array
-
Merge Sorted Array II
-
Median
-
Partition Array by Odd and Even
-
Kth Largest Element
-
Remove Element
-
Binary Search
-
First Position of Target
-
Search Insert Position
-
Search for a Range
-
First Bad Version
-
Search a 2D Matrix
-
Search a 2D Matrix II
-
Find Peak Element
-
Search in Rotated Sorted Array
-
Search in Rotated Sorted Array II
-
Find Minimum in Rotated Sorted Array
-
Find Minimum in Rotated Sorted Array II
-
Median of two Sorted Arrays
-
Sqrt x
-
Wood Cut
-
First Position of Target
-
Math and Bit Manipulation
-
Single Number
-
Single Number II
-
Single Number III
-
O1 Check Power of 2
-
Convert Integer A to Integer B
-
Factorial Trailing Zeroes
-
Unique Binary Search Trees
-
Update Bits
-
Fast Power
-
Hash Function
-
Happy Number
-
Count 1 in Binary
-
Fibonacci
-
A plus B Problem
-
Print Numbers by Recursion
-
Majority Number
-
Majority Number II
-
Majority Number III
-
Digit Counts
-
Ugly Number
-
Plus One
-
Palindrome Number
-
Task Scheduler
-
Single Number
-
Linked List
-
Remove Duplicates from Sorted List
-
Remove Duplicates from Sorted List II
-
Remove Duplicates from Unsorted List
-
Partition List
-
Add Two Numbers
-
Two Lists Sum Advanced
-
Remove Nth Node From End of List
-
Linked List Cycle
-
Linked List Cycle II
-
Reverse Linked List
-
Reverse Linked List II
-
Merge Two Sorted Lists
-
Merge k Sorted Lists
-
Reorder List
-
Copy List with Random Pointer
-
Sort List
-
Insertion Sort List
-
Palindrome Linked List
-
LRU Cache
-
Rotate List
-
Swap Nodes in Pairs
-
Remove Linked List Elements
-
Remove Duplicates from Sorted List
-
Binary Tree
-
Binary Tree Preorder Traversal
-
Binary Tree Inorder Traversal
-
Binary Tree Postorder Traversal
-
Binary Tree Level Order Traversal
-
Binary Tree Level Order Traversal II
-
Maximum Depth of Binary Tree
-
Balanced Binary Tree
-
Binary Tree Maximum Path Sum
-
Lowest Common Ancestor
-
Invert Binary Tree
-
Diameter of a Binary Tree
-
Construct Binary Tree from Preorder and Inorder Traversal
-
Construct Binary Tree from Inorder and Postorder Traversal
-
Subtree
-
Binary Tree Zigzag Level Order Traversal
-
Binary Tree Serialization
-
Binary Tree Preorder Traversal
- Binary Search Tree
- Exhaustive Search
-
Dynamic Programming
-
Triangle
-
Backpack
-
Backpack II
-
Minimum Path Sum
-
Unique Paths
-
Unique Paths II
-
Climbing Stairs
-
Jump Game
-
Word Break
-
Longest Increasing Subsequence
-
Palindrome Partitioning II
-
Longest Common Subsequence
-
Edit Distance
-
Jump Game II
-
Best Time to Buy and Sell Stock
-
Best Time to Buy and Sell Stock II
-
Best Time to Buy and Sell Stock III
-
Best Time to Buy and Sell Stock IV
-
Distinct Subsequences
-
Interleaving String
-
Maximum Subarray
-
Maximum Subarray II
-
Longest Increasing Continuous subsequence
-
Longest Increasing Continuous subsequence II
-
Maximal Square
-
Triangle
- Graph
- Data Structure
- Big Data
- Problem Misc
-
Part III - Contest
- Google APAC
- Microsoft
- Appendix I Interview and Resume
-
Tags
Palindrome Partitioning
- tags: [palindrome]
Question
- leetcode: Palindrome Partitioning | LeetCode OJ
- lintcode: (136) 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"]
]
copy
题解1 - DFS
罗列所有可能,典型的 DFS. 此题要求所有可能的回文子串,即需要找出所有可能的分割,使得分割后的子串都为回文。凭借高中的排列组合知识可知这可以用『隔板法』来解决,具体就是在字符串的每个间隙为一个隔板,对于长度为 n 的字符串,共有 n-1 个隔板可用,每个隔板位置可以选择放或者不放,总共有 种可能。由于需要满足『回文』条件,故实际上需要穷举的状态数小于 .
回溯法看似不难,但是要活学活用起来还是不容易的,核心抓住两点:深搜的递归建立和剪枝函数的处理。
根据『隔板法』的思想,我们首先从第一个隔板开始挨个往后取,若取到的子串不是回文则立即取下一个隔板,直到取到最后一个隔板。若取到的子串是回文,则将当前子串加入临时列表中,接着从当前隔板处字符开始递归调用回溯函数,直至取到最后一个隔板,最后将临时列表中的子串加入到最终返回结果中。接下来则将临时列表中的结果一一移除,这个过程和 subsets 模板很像,代码比这个文字描述更为清晰。
Python
class Solution:
# @param s, a string
# @return a list of lists of string
def partition(self, s):
result = []
if not s:
return result
palindromes = []
self.dfs(s, 0, palindromes, result)
return result
def dfs(self, s, pos, palindromes, ret):
if pos == len(s):
ret.append([] + palindromes)
return
for i in xrange(pos + 1, len(s) + 1):
if not self.isPalindrome(s[pos:i]):
continue
palindromes.append(s[pos:i])
self.dfs(s, i, palindromes, ret)
palindromes.pop()
def isPalindrome(self, s):
if not s:
return False
# reverse compare
return s == s[::-1]
copy
C++
class Solution {
public:
/**
* @param s: A string
* @return: A list of lists of string
*/
vector<vector<string>> partition(string s) {
vector<vector<string> > result;
if (s.empty()) return result;
vector<string> palindromes;
dfs(s, 0, palindromes, result);
return result;
}
private:
void dfs(string s, int pos, vector<string> &palindromes,
vector<vector<string> > &ret) {
if (pos == s.size()) {
ret.push_back(palindromes);
return;
}
for (int i = pos + 1; i <= s.size(); ++i) {
string substr = s.substr(pos, i - pos);
if (!isPalindrome(substr)) {
continue;
}
palindromes.push_back(substr);
dfs(s, i, palindromes, ret);
palindromes.pop_back();
}
}
bool isPalindrome(string s) {
if (s.empty()) return false;
int n = s.size();
for (int i = 0; i < n; ++i) {
if (s[i] != s[n - i - 1]) return false;
}
return true;
}
};
copy
Java
public class Solution {
/**
* @param s: A string
* @return: A list of lists of string
*/
public List<List<String>> partition(String s) {
List<List<String>> result = new ArrayList<List<String>>();
if (s == null || s.isEmpty()) return result;
List<String> palindromes = new ArrayList<String>();
dfs(s, 0, palindromes, result);
return result;
}
private void dfs(String s, int pos, List<String> palindromes,
List<List<String>> ret) {
if (pos == s.length()) {
ret.add(new ArrayList<String>(palindromes));
return;
}
for (int i = pos + 1; i <= s.length(); i++) {
String substr = s.substring(pos, i);
if (!isPalindrome(substr)) {
continue;
}
palindromes.add(substr);
dfs(s, i, palindromes, ret);
palindromes.remove(palindromes.size() - 1);
}
}
private boolean isPalindrome(String s) {
if (s == null || s.isEmpty()) return false;
int n = s.length();
for (int i = 0; i < n; i++) {
if (s.charAt(i) != s.charAt(n - i - 1)) return false;
}
return true;
}
}
copy
源码分析
回文的判断采用了简化的版本,没有考虑空格等非字母数字字符要求。Java 中 ArrayList 和 List 的实例化需要注意下。Python 中 result 的初始化为[], 不需要初始化为 [[]] 画蛇添足。C++ 中的.substr(pos, n)
含义为从索引为 pos 的位置往后取 n 个(含) 字符,注意与 Java 中区别开来。
复杂度分析
DFS,状态数最多 , 故时间复杂度为 , 使用了临时列表,空间复杂度为 .
Reference
- Palindrome Partitioning 参考程序 Java/C++/Python
- soulmachine 的 Palindrome Partitioning