[LeetCode] Palindrome Partitioning

简介: The problem has a nice structure that backtracking naturally fits in. The structure is, given a starting position idx, we search from idx till the end of the string s.

The problem has a nice structure that backtracking naturally fits in. The structure is, given a starting position idx, we search from idx till the end of the string s.length() - 1. Once we reach a position i such that the sub-string from idx to i (s.substr(idx, i - idx + 1)) is a palindrome, we add it to a temporary tmp. Then we recursively call the same function to process the remaining sub-string. Once we reach the end of the string, we add tmp into the result res of all the possible partitioning.

Then, backtracking happens! Remember that at position i, we find s.substr(idx, i - idx + 1) to be a palindrome and we immediately add it to tmp. It is obvious that there may be some position j such that j > i and s.substr(idx, j - idx + 1) is also a palindrome. So we need to recover to the state before adding s.substr(idx, i - idx + 1) to tmp and continue to find the next palindrome position after i. And we simply need to pop s.substr(idx, i - idx + 1)out of tmp to make things work.

Putting these together, we can write down the following code, which should be self-explanatory.

 1 class Solution {
 2 public:
 3     vector<vector<string>> partition(string s) {
 4         vector<vector<string>> res;
 5         vector<string> tmp;
 6         getPartition(s, 0, tmp, res);
 7         return res;
 8     }
 9 private: 
10     void getPartition(string& s, int idx, vector<string>& tmp, vector<vector<string>>& res) {
11         if (idx == s.length()) {
12             res.push_back(tmp);
13             return;
14         }
15         for (int i = idx, n = s.length(); i < n; i++) {
16             int l = idx, r = i;
17             while (l < r && s[l] == s[r]) l++, r--;
18             if (l >= r) {
19                 tmp.push_back(s.substr(idx, i - idx + 1));
20                 getPartition(s, i + 1, tmp, res);
21                 tmp.pop_back();
22             }
23         }
24     }
25 };

 

目录
相关文章
LeetCode 409. Longest Palindrome
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
78 0
LeetCode 409. Longest Palindrome
|
索引
LeetCode 336. Palindrome Pairs
给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。
139 0
LeetCode 336. Palindrome Pairs
|
算法 索引
LeetCode 214. Shortest Palindrome
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
92 0
LeetCode 214. Shortest Palindrome
|
存储
LeetCode 132. Palindrome Partitioning II
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。 返回符合要求的最少分割次数。
102 0
LeetCode 132. Palindrome Partitioning II
LeetCode 131. Palindrome Partitioning
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。 返回 s 所有可能的分割方案。
96 0
LeetCode 131. Palindrome Partitioning
|
canal
LeetCode 125. Valid Palindrome
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
94 0
LeetCode 125. Valid Palindrome
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
Leetcode-Easy 234. Palindrome Linked List
Leetcode-Easy 234. Palindrome Linked List
59 0
Leetcode-Easy 234. Palindrome Linked List
【LeetCode】Palindrome Pairs(336)
  Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a   palindrome.
115 0
LeetCode之Palindrome Number(回文数)
LeetCode之Palindrome Number(回文数)
72 0