palindrome

Add the least amount of characters to make a palindrome

半腔热情 提交于 2019-11-30 14:05:00
The question: Given any string, add the least amount of characters possible to make it a palindrome in linear time. I'm only able to come up with a O(N 2 ) solution. Can someone help me with an O(N) solution? Chronial Revert the string Use a modified Knuth-Morris-Pratt to find the latest match (simplest modification would be to just append the original string to the reverted string and ignore matches after len(string). Append the unmatched rest of the reverted string to the original. 1 and 3 are obviously linear and 2 is linear beacause Knuth-Morris-Pratt is. If only appending is allowed A

Manacher's algorithm (algorithm to find longest palindrome substring in linear time)

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 10:08:26
问题 After spending about 6-8 hours trying to digest the Manacher's algorithm, I am ready to throw in the towel. But before I do, here is one last shot in the dark: can anyone explain it? I don't care about the code. I want somebody to explain the ALGORITHM . Here seems to be a place that others seemed to enjoy in explaining the algorithm: http://www.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html I understand why you would want to transform the string, say, 'abba' to #a#b#b#a#

Convert string to palindrome string with minimum insertions

家住魔仙堡 提交于 2019-11-30 09:02:20
In order to find the minimal number of insertions required to convert a given string(s) to palindrome I find the longest common subsequence of the string(lcs_string) and its reverse. Therefore the number of insertions to be made is length(s) - length(lcs_string) What method should be employed to find the equivalent palindrome string on knowing the number of insertions to be made? For example : 1) azbzczdzez Number of insertions required : 5 Palindrome string : azbzcezdzeczbza Although multiple palindrome strings may exist for the same string but I want to find only one palindrome? Let S[i, j]

Understanding difference lists (Prolog)

空扰寡人 提交于 2019-11-30 05:45:35
问题 I'm having trouble understanding difference list, particularly in this predicate: palindrome(A, A). palindrome([_|A], A). palindrome([C|A], D) :- palindrome(A, B), B=[C|D]. Could anyone help me follow what's happening? 回答1: palindrome(A, A). palindrome([_|A], A). palindrome([C|A], D) :- palindrome(A, B), B=[C|D]. Seeing the arguments to this predicate as a difference list, the first clause says, a list from A to A (i.e., an empty list) is a palindrome. The second clause says, a one-element

How to find the longest palindrome in a given string? [duplicate]

北慕城南 提交于 2019-11-30 04:08:04
This question already has an answer here: Write a function that returns the longest palindrome in a given string 22 answers Possible Duplicate: Write a function that returns the longest palindrome in a given string I know how to do this in O(n^2). But it seems like there exist a better solution. I've found this , and there is a link to O(n) answer, but it's written in Haskell and not clear for me. It would be great to get an answer in c# or similar. I've found clear explanation of the solution here . Thanks to Justin for this link. There you can find Python and Java implementations of the

Next higher prime and palindrome number

夙愿已清 提交于 2019-11-29 23:06:01
问题 Is there any suggestion on solving next higher prime and palindrome number from a given int. Here is the snippet I am trying but its a kind of slow, please suggest if you ve any good algorithm that i can test. #!/usr/bin/python def next_higher(n): while True: s = str(n) if not any([n % i == 0 \ for i in range(2, int(n**0.5))]) and s == s[::-1]: return n n = n + 1 print next_higher(2004) print next_higher(20) Output: 10201 101 Updated code testing for palindrome before prime. much faster than

Palindrome Golf

为君一笑 提交于 2019-11-29 19:45:13
The goal: Any language. The smallest function which will return whether a string is a palindrome. Here is mine in Python : R=lambda s:all(a==b for a,b in zip(s,reversed(s))) 50 characters. The accepted answer will be the current smallest one - this will change as smaller ones are found. Please specify the language your code is in. 7 characters in J: Not sure if this is the best way, I'm somewhat new to J :) p=:-:|. explanation: |. reverses the input. -: compares. the operands are implicit. p 'radar' 1 p 'moose' 0 Menkboy Here's mine; it's written in a domain-specific language I invented,

Manacher's algorithm (algorithm to find longest palindrome substring in linear time)

試著忘記壹切 提交于 2019-11-29 19:16:11
After spending about 6-8 hours trying to digest the Manacher's algorithm, I am ready to throw in the towel. But before I do, here is one last shot in the dark: can anyone explain it? I don't care about the code. I want somebody to explain the ALGORITHM . Here seems to be a place that others seemed to enjoy in explaining the algorithm: http://www.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html I understand why you would want to transform the string, say, 'abba' to #a#b#b#a# After than I'm lost. For example, the author of the previously mentioned website says the key part of the

Python reverse() for palindromes

五迷三道 提交于 2019-11-29 13:56:52
I'm just getting started in python, and I'm trying to test a user-entered string as a palindrome. My code is: x=input('Please insert a word') y=reversed(x) if x==y: print('Is a palindrome') else: print('Is not a palindrome') This always returns false because y becomes something like <reversed object at 0x00E16EF0> instead of the reversed string. What am I being ignorant about? How would you go about coding this problem? Try y = x[::-1] . This uses splicing to get the reverse of the string. reversed(x) returns an iterator for looping over the characters in the string in reverse order, not a

Create palindrome from existing string by removing characters

送分小仙女□ 提交于 2019-11-29 05:01:22
How do i determine the length of the longest palindrome you can get from a word by removing zero or more letters. for eg : amanQQQapl12345anacaZZZnalpaXXXna67890ma longest palindrome will be of 21 digits. Rambo This can be solved by dynamic programming. Define d[i, j] as the length of longest palindrome in the original string. If s[i] = s[j], d[i, j] = max(d[i+1, j-1] + 2, d[i, j-1], d[i+1, j]). Otherwise d[i, j] = max(d[i, j-1], d[i+1, j]). The longest palindrome in the word W is the longest common subsequence of W and its mirror. You can compute it in O(n²) time and O(n) space where n is the