palindrome

how to find longest palindromic subsequence?

浪尽此生 提交于 2019-11-27 02:54:19
Here is the problem (6.7 ch6 ) from Algorithms book (by Vazirani) that slightly differs from the classical problem that finding longest palindrome . How can I solve this problem ? A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence A,C,G,T,G,T,C,A,A,A,A,T,C,G has many palindromic subsequences, including A,C,G,C,A and A,A,A,A (on the other hand, the subsequence A,C,T is not palindromic). Devise an algorithm that takes a sequence x[1 ...n] and returns the (length of the) longest palindromic subsequence. Its running time should be

Find all substrings that are palindromes

不羁的心 提交于 2019-11-27 00:23:29
问题 If the input is 'abba' then the possible palindromes are a, b, b, a, bb, abba. I understand that determining if string is palindrome is easy. It would be like: public static boolean isPalindrome(String str) { int len = str.length(); for(int i=0; i<len/2; i++) { if(str.charAt(i)!=str.charAt(len-i-1) { return false; } return true; } But what is the efficient way of finding palindrome substrings? 回答1: This can be done in O(n) , using Manacher's algorithm. The main idea is a combination of

Check if a string is a palindrome

扶醉桌前 提交于 2019-11-26 22:46:27
问题 I have a string as input and have to break the string in two substrings. If the left substring equals the right substring then do some logic. How can I do this? Sample: public bool getStatus(string myString) { } Example: myString = "ankYkna" , so if we break it into two substring it would be: left-part = "ank" , right-part = "ank" (after reversal). 回答1: public static bool getStatus(string myString) { string first = myString.Substring(0, myString.Length / 2); char[] arr = myString.ToCharArray(

Longest palindrome in a string using suffix tree

我只是一个虾纸丫 提交于 2019-11-26 22:29:50
问题 I was trying to find the longest palindrome in a string. The brute force solution takes O(n^3) time. I read that there is a linear time algorithm for it using suffix trees. I am familiar with suffix trees and am comfortable building them. How do you use the built suffix tree to find the longest palindrome. 回答1: I believe you need to proceed this way: Let y 1 y 2 ... y n be your string (where y i are letters). Create the generalized suffix tree of S f = y 1 y 2 ... y n $ and S r = y n y n - 1

Creating a recursive method for Palindrome

好久不见. 提交于 2019-11-26 22:23:15
问题 I am trying to create a Palindrome program using recursion within Java but I am stuck, this is what I have so far: public static void main (String[] args){ System.out.println(isPalindrome("noon")); System.out.println(isPalindrome("Madam I'm Adam")); System.out.println(isPalindrome("A man, a plan, a canal, Panama")); System.out.println(isPalindrome("A Toyota")); System.out.println(isPalindrome("Not a Palindrome")); System.out.println(isPalindrome("asdfghfdsa")); } public static boolean

Python: search longest palindromes within a word and palindromes within a word/string

佐手、 提交于 2019-11-26 20:50:16
问题 So here is a code i have written to find palindromes within a word (To check if there are palindromes within a word including the word itself) Condition: spaces inbetween characters are counted and not ignored Example: A but tuba is a palindrome but technically due to spaces involved now it isn't. so that's the criteria. Based on above, the following code usually should work. You can try on your own with different tests to check out if this code gives any error. def pal(text): """ param text:

How does this PCRE pattern detect palindromes?

我是研究僧i 提交于 2019-11-26 20:35:15
问题 This question is an educational demonstration of the usage of lookahead, nested reference, and conditionals in a PCRE pattern to match ALL palindromes, including the ones that can't be matched by the recursive pattern given in the PCRE man page. Examine this PCRE pattern in PHP snippet: $palindrome = '/(?x) ^ (?: (.) (?= .* ( \1 (?(2) \2 | ) ) $ ) )* .? \2? $ /'; This pattern seems to detect palindromes, as seen in this test cases (see also on ideone.com): $tests = array( # palindromes '', 'a

How does this Java regex detect palindromes?

房东的猫 提交于 2019-11-26 16:25:11
问题 This is the third part in a series of educational regex articles. It follows How does this regex find triangular numbers? (where nested references is first introduced) and How can we match a^n b^n with Java regex? (where the lookahead "counting" mechanism is further elaborated upon). This part introduces a specific form of nested assertion, which when combined with nested references allows Java regex to match what most people believe is "impossible": palindromes!! The language of palindromes

How to write palindrome in JavaScript

僤鯓⒐⒋嵵緔 提交于 2019-11-26 12:40:56
问题 I wonder how to write palindrome in javascript, where I input different words and program shows if word is palindrome or not. For example word noon is palindrome, while bad is not. Thank you in advance. 回答1: function palindrome(str) { var len = str.length; var mid = Math.floor(len/2); for ( var i = 0; i < mid; i++ ) { if (str[i] !== str[len - 1 - i]) { return false; } } return true; } palindrome will return if specified word is palindrome, based on boolean value (true/false) UPDATE: I opened

A better algorithm to find the next palindrome of a number string

我只是一个虾纸丫 提交于 2019-11-26 12:23:52
问题 Firstly here is the problem: A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros. Input: The first line contains integer t, the number of test cases. Integers K are given in the next t lines. Output: For each K,