# Palindrome Partitioning
- tags: [palindrome]
### Source
- leetcode: [Palindrome Partitioning | LeetCode OJ](https://leetcode.com/problems/palindrome-partitioning/)
- lintcode: [(136) Palindrome Partitioning](http://www.lintcode.com/en/problem/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"]
]
~~~
### 题解1 - [DFS](# "Depth-First Search, 深度优先搜索")
罗列所有可能,典型的 [DFS](# "Depth-First Search, 深度优先搜索"). 此题要求所有可能的回文子串,即需要找出所有可能的分割,使得分割后的子串都为回文。凭借高中的排列组合知识可知这可以用『隔板法』来解决,具体就是在字符串的每个间隙为一个隔板,对于长度为 n 的字符串,共有 n-1 个隔板可用,每个隔板位置可以选择放或者不放,总共有 O(2n−1)O(2^{n-1})O(2n−1) 种可能。由于需要满足『回文』条件,故实际上需要穷举的状态数小于 O(2n−1)O(2^{n-1})O(2n−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]
~~~
### 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;
}
};
~~~
### 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;
}
}
~~~
### 源码分析
回文的判断采用了简化的版本,没有考虑空格等非字母数字字符要求。Java 中 ArrayList 和 List 的实例化需要注意下。Python 中 result 的初始化为[], 不需要初始化为 [[]] 画蛇添足。C++ 中的`.substr(pos, n)` 含义为从索引为 pos 的位置往后取 n 个(含) 字符,注意与 Java 中区别开来。
### 复杂度分析
[DFS](# "Depth-First Search, 深度优先搜索"),状态数最多 O(2n−1)O(2^{n-1})O(2n−1), 故时间复杂度为 O(2n)O(2^n)O(2n), 使用了临时列表,空间复杂度为 O(n)O(n)O(n).
### Reference
- [Palindrome Partitioning 参考程序 Java/C++/Python](http://www.jiuzhang.com/solutions/palindrome-partitioning/)
- soulmachine 的 Palindrome Partitioning
- Preface
- Part I - Basics
- Basics Data Structure
- String
- Linked List
- Binary Tree
- Huffman Compression
- Queue
- Heap
- Stack
- Set
- Map
- Graph
- Basics Sorting
- Bubble Sort
- Selection Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Heap Sort
- Bucket Sort
- Counting Sort
- Radix Sort
- Basics Algorithm
- Divide and Conquer
- Binary Search
- Math
- Greatest Common Divisor
- Prime
- Knapsack
- Probability
- Shuffle
- Basics Misc
- Bit Manipulation
- Part II - Coding
- String
- strStr
- Two Strings Are Anagrams
- Compare Strings
- Anagrams
- Longest Common Substring
- Rotate String
- Reverse Words in a String
- Valid Palindrome
- Longest Palindromic Substring
- Space Replacement
- Wildcard Matching
- Length of Last Word
- Count and Say
- 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
- Binary Search
- Binary Search
- 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
- 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
- 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
- Linked List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List II
- Remove Duplicates from Unsorted List
- Partition List
- Two Lists Sum
- 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
- Check if a singly linked list is palindrome
- Delete Node in the Middle of Singly Linked List
- Rotate List
- Swap Nodes in Pairs
- Remove Linked List Elements
- 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 Search Tree
- Insert Node in a Binary Search Tree
- Validate Binary Search Tree
- Search Range in Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Binary Search Tree Iterator
- Exhaustive Search
- Subsets
- Unique Subsets
- Permutations
- Unique Permutations
- Next Permutation
- Previous Permuation
- Unique Binary Search Trees II
- Permutation Index
- Permutation Index II
- Permutation Sequence
- Palindrome Partitioning
- Combinations
- Combination Sum
- Combination Sum II
- Minimum Depth of Binary Tree
- Word 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
- Graph
- Find the Connected Component in the Undirected Graph
- Route Between Two Nodes in Graph
- Topological Sorting
- Word Ladder
- Bipartial Graph Part I
- Data Structure
- Implement Queue by Two Stacks
- Min Stack
- Sliding Window Maximum
- Longest Words
- Heapify
- Problem Misc
- Nuts and Bolts Problem
- String to Integer
- Insert Interval
- Merge Intervals
- Minimum Subarray
- Matrix Zigzag Traversal
- Valid Sudoku
- Add Binary
- Reverse Integer
- Gray Code
- Find the Missing Number
- Minimum Window Substring
- Continuous Subarray Sum
- Continuous Subarray Sum II
- Longest Consecutive Sequence
- Part III - Contest
- Google APAC
- APAC 2015 Round B
- Problem A. Password Attacker
- Microsoft
- Microsoft 2015 April
- Problem A. Magic Box
- Problem B. Professor Q's Software
- Problem C. Islands Travel
- Problem D. Recruitment
- Microsoft 2015 April 2
- Problem A. Lucky Substrings
- Problem B. Numeric Keypad
- Problem C. Spring Outing
- Microsoft 2015 September 2
- Problem A. Farthest Point
- Appendix I Interview and Resume
- Interview
- Resume