Question

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

Solution

基本思路还是递归。每次只探究第一刀切在哪儿。这里,为了避免重复计算,我们用DP来先处理子串是否对称的问题。思路参见 Palindrom Subarrays

如果想进一步节省时间,可以参见Word Break II的解法,将每个子问题的解存起来。

 class Solution(object):
def construction(self, s):
length = len(s)
self.dp = [[False for i in range(length)] for j in range(length)]
for i in range(length):
self.dp[i][i] = True
for i in range(length - 1):
if s[i] == s[i + 1]:
self.dp[i][i + 1] = True
for sub_len in range(3, length + 1):
for start in range(0, length - sub_len + 1):
end = start + sub_len - 1
if s[start] == s[end] and self.dp[start + 1][end - 1]:
self.dp[start][end] = True def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
self.construction(s)
result = []
self.helper(s, 0, [], result)
return result def helper(self, s, start, cur_list, result):
length = len(s)
if start == length:
result.append(list(cur_list))
return
for end in range(start, length):
if self.dp[start][end]:
cur_list.append(s[start : end + 1])
self.helper(s, end + 1, cur_list, result)
cur_list.pop()

由于Python本身对字符串的强大处理,这道题的解答也可以为:

(比上一个解法花时间多)

 class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
return [[s[:i]] + rest
for i in xrange(1, len(s)+1)
if s[:i] == s[i-1::-1]
for rest in self.partition(s[i:])] or [[]]

Palindrome Partitioning 解答的更多相关文章

  1. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  2. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  3. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  4. LintCode Palindrome Partitioning II

    Given a string s, cut s into some substrings such that every substring is a palindrome. Return the m ...

  5. LeetCode(131)Palindrome Partitioning

    题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  6. Leetcode 131. Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  7. Palindrome Partitioning II Leetcode

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  8. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. [Leetcode] Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. (Data structure)Implement Trie && Add and Search Word

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  2. paip.sql索引优化----join 代替子查询法

    paip.sql索引优化----join 代替子查询法 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.n ...

  3. linux文件系统学习

    linux系统支持很多种文件系统. 1. 如何确认当前系统挂载了哪些文件系统? 使用mount命令可以查看当前系统上已经挂载了哪些文件系统, sh-# mount rootfs on / type r ...

  4. hdu1047(模拟大量的循环添加)

    标题信息:总结多个大整数,(使用add循环相加的功能) http://acm.hdu.edu.cn/showproblem.php? pid=1047 AC代码: /**  *大数的循环加法,转化为字 ...

  5. DataBindings 与 INotifyPropertyChanged 实现自动刷新 WinForm 界面

    --首发于博客园, 转载请保留此链接  博客原文地址 业务逻辑与界面的分离对于维护与迁移是非常重要的,在界面上给某属性赋值,后台要检测到其已经发生变化 问题: 输入某物品 单价 Price, 数量Am ...

  6. RMAN的show,list,crosscheck,delete命令

    1.SHOW命令:      显示rman配置: RMAN> show all; 2.REPORT命令: 2.1.RMAN> report schema 报告目标数据库的物理结构; 2.2 ...

  7. Notification (通知)的 新版和旧版用法

    Notification (通知)的 新版和旧版用法   一.先来看旧版,Api 11 之前的用法: NotificationManager manager = (NotificationManage ...

  8. Verilog 读写文件

    Verilog 读写文件 在数字设计验证中,有时我们需要大量的数据,这时可以通过文件输入,有时我们需要保存数据,可以通过写文件保存. 读写文件testbench module file_rw_tb() ...

  9. DropDownList绑定数据

    DDLName.DataSource = myRd;DDLName.DataTextField = "name";//要绑定的字段DDLName.DataValueField = ...

  10. oracle 的服务器进程(PMON, SMON,CKPT,DBWn,LGWR,ARCn)

    来着TOM的<oracle 编程艺术 9i,10g,11g> PMON PMON,进程监视.PMON主要有3个用途: 1,在进程非正常中断后,做清理工作.例如:dedicated serv ...