Palindrome Subarrays

给定输入字符串,要求判断任意子字符串是否对称。

基本思路就是DP

写出DP表达式为 dp[i][j] = dp[i + 1][j - 1] && (s[i] == s[j])    dp[i][j]代表s(i,j)的子串是否对称

注意在for循环赋值时,这里增长的对象其实是子串的长度。

长度为奇数时:

(->代表决定)

dp[0,0]

dp[1,1] -> dp[0,2]

dp[2,2] -> dp[1,3] -> dp[0,4]

dp[3,3] -> dp[2,4] -> dp[1, 5]

...

长度为偶数时:

dp[0,1]

dp[1,2] -> dp[0,3]

dp[2,3] -> dp[1,4] -> dp[0,5]

dp[3,4] -> dp[2,5] -> dp[1,6]

...

因此外层循环变量是长度,内层循环变量是起点

 1 def palindrom(s):
 2     length = len(s)
 3     # Create a 2D array in Python
 4     dp = [[False for i in range(length)] for j in range(length)]
 5     # length = 1 substring
 6     for i in range(length):
 7         dp[i][i] = True
 8     # length = 2 substring
 9     for i in range(length - 1):
10         if s[i] == s[i + 1]:
11             dp[i][i + 1] = True
12     # length = k substring
13     for len3 in range(3, length + 1):
14         for start in range(0, length - len3 + 1):
15             end = start + len3 - 1
16             if s[start] == s[end] and dp[start + 1][end - 1]:
17                 dp[start][end] = True
18
19     print dp
20     print dp[0][length - 1]
21
22 palindrom(‘aaaaabbb‘)
时间: 2024-10-11 04:30:26

Palindrome Subarrays的相关文章

HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加字符的个数. 题解1(LCS): 很神奇的做法. 先求s和s的反串的LCS,也就是原串中已经满足回文性质的字符个数. 然后要变成回文串的话,只需要为剩下的每个落单的字符,相应地插入一个和它相同的字符即可. 所以答案是:s.size()-LCS(s,rev(s)) 另外,求LCS时只会用到lcs[i-

Palindrome Linked List Leetcode

Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 这个follow up要求O(n)的时间和O(1)的空间,可以先reverse一半,然后再对比.只是reverse的时候要考虑奇数个还是偶数个.如果是奇数个的话,就跳过最中间的,从下一个开始reverse. /** * Definition for singly-li

LeetCode 9 Palindrome Number (回文数)

翻译 确定一个整数是否是回文数.不能使用额外的空间. 一些提示: 负数能不能是回文数呢?(比如,-1) 如果你想将整数转换成字符串,但要注意限制使用额外的空间. 你也可以考虑翻转一个整数. 然而,如果你已经解决了问题"翻转整数(译者注:LeetCode 第七题), 那么你应该知道翻转的整数可能会造成溢出. 你将如何处理这种情况? 这是一个解决该问题更通用的方法. 原文 Determine whether an integer is a palindrome. Do this without ex

Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 思路: 把链表一分为二,把右边的一半翻转,再逐个比对左右的链表即可. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo

[LeetCode]132.Palindrome Partitioning II

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu

[LeetCode]Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider that

leetcode -- Palindrome Partitioning II

指责别人,看清自己 [问题描述] Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning

poj1159 Palindrome(最长公共子序列)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52966   Accepted: 18271 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a

Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th