palindrome

LeetCode:Valid Palindrome

牧云@^-^@ 提交于 2019-11-29 04:43:20
1、题目名称 Valid Palindrome(回文字符串) 2、题目地址 https://leetcode.com/problems/valid-palindrome/ 3、题目内容 英文:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. 中文:给出一个字符串,只考虑字母和数字,判断该字符串是否为回文字符串 例如: "A man, a plan, a canal: Panama" 是一个回文字符串 "race a car" 不是一个回文字符串 4、题目分析 如果要判断一个字符串是否是回文字符串,可以两个变量,从两侧开始比较有效的字符是否相等。这里有效的字符是指 'a'-'z'、'A'-'Z'、'0'-'9' 这三个区间内的字符。 5、解题方法1 可以将字符串中有效的字符,写入一个链表,再将链表转化为数组,直接判断数组两侧对称位置的字符是否匹配。 实现的Java代码如下: import java.util.LinkedList; /** * 功能说明:LeetCode 125 - Valid Palindrome * 开发人员:Tsybius2014 * 开发时间:2015年8月4日 */ public class

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

妖精的绣舞 提交于 2019-11-29 01:58:46
问题 This question already has answers here : Write a function that returns the longest palindrome in a given string (22 answers) Closed 6 years ago . 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. 回答1: I've found clear

Palindrome detection efficiency

戏子无情 提交于 2019-11-28 20:40:14
I got curious by Jon Limjap's interview mishap and started to look for efficient ways to do palindrome detection. I checked the palindrome golf answers and it seems to me that in the answers are two algorithms only, reversing the string and checking from tail and head. def palindrome_short(s): length = len(s) for i in xrange(0,length/2): if s[i] != s[(length-1)-i]: return False return True def palindrome_reverse(s): return s == s[::-1] I think neither of these methods are used in the detection of exact palindromes in huge DNA sequences. I looked around a bit and didn't find any free article

Palindrome Golf

孤人 提交于 2019-11-28 15:33:49
问题 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. 回答1: 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

Check if a string is palindrome in C

眉间皱痕 提交于 2019-11-28 12:57:34
问题 i've a question about this code i'm writing for an exercise. I've to check if a string is palindrome. I can't change the declaration of the function.The function only return 1 when all the letters are the same (like "aaaa") but if i charge the sentence with other palindrome (like "anna") the function return me 0 , i can't figure out why this appening.Thank you! char* cargar (char*); int pali (char*); int main() { char*texto=NULL; texto=cargar(texto); int res=pali(texto); if(res==1){printf("

Largest palindrome product - euler project

你说的曾经没有我的故事 提交于 2019-11-28 11:50:34
I was trying to solve project Euler problem 4 which is: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. Here is my solution , it's output is 997799 , however that's not the correct answer ,I wonder where is the problem: package projecteuler; public class Pro4 { public static void main(String[] args) { for(int i=999*999;i>=100*100;i--){ if(isPalindrome(i)==true){ System.out.println(i); break; } } } static boolean isPalindrome(int x){ int

How to check if the binary representation of an integer is a palindrome?

≯℡__Kan透↙ 提交于 2019-11-28 09:24:05
How to check if the binary representation of an integer is a palindrome? Since you haven't specified a language in which to do it, here's some C code (not the most efficient implementation, but it should illustrate the point): /* flip n */ unsigned int flip(unsigned int n) { int i, newInt = 0; for (i=0; i<WORDSIZE; ++i) { newInt += (n & 0x0001); newInt <<= 1; n >>= 1; } return newInt; } bool isPalindrome(int n) { int flipped = flip(n); /* shift to remove trailing zeroes */ while (!(flipped & 0x0001)) flipped >>= 1; return n == flipped; } EDIT fixed for your 10001 thing. Hopefully correct:

Find all substrings that are palindromes

烂漫一生 提交于 2019-11-28 04:24:37
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? Michał Rybak This can be done in O(n) , using Manacher's algorithm . The main idea is a combination of dynamic programming and (as others have said already) computing maximum length of palindrome with

Longest palindrome in a string using suffix tree

我怕爱的太早我们不能终老 提交于 2019-11-28 02:49:11
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. 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 ... y 1 # (reverse the letters and choose different ending characters for S f ($) and S r (#))... where S f

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

北城余情 提交于 2019-11-27 22:38:32
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: given string or test return: returns index of longest palindrome and a list of detected palindromes