Questions tagged [palindrome]

A word, phrase, number, or other sequence of units that may be read the same way in either direction, forward or backward.

A word, phrase, number, or other sequence of units that may be read the same way in either direction, forward or backward.

Spacing and punctuation is generally ignored when attempting to determine whether the potential palindrome under consideration is in fact a palindrome. One such example is:

I, madam, I, made radio - so I dared! Am I mad? Am I?

(from http://www.joe-ks.com/palindromes.htm)

1763 questions
109
votes
32 answers

How to check that a string is a palindrome using regular expressions?

That was an interview question that I was unable to answer: How to check that a string is a palindrome using regular expressions? p.s. There is already a question "How to check if the given string is palindrome?" and it gives a lot of answers in…
Degvik
  • 3,050
  • 6
  • 25
  • 20
104
votes
23 answers

Write a function that returns the longest palindrome in a given string

e.g "ccddcc" in the string "abaccddccefe" I thought of a solution but it runs in O(n^2) time Algo 1: Steps: Its a brute force method Have 2 for loops for i = 1 to i less than array.length -1 for j=i+1 to j less than array.length This way you…
Learner
  • 2,556
  • 11
  • 33
  • 38
94
votes
43 answers

Check string for palindrome

A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction. To check whether a word is a palindrome I get the char array of the word and compare the chars. I tested it and it seems to work.…
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
73
votes
9 answers

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

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…
user678392
  • 1,981
  • 3
  • 28
  • 50
57
votes
35 answers

How to check for palindrome using Python logic

I'm trying to check for a palindrome with Python. The code I have is very for-loop intensive. And it seems to me the biggest mistake people do when going from C to Python is trying to implement C logic using Python, which makes things run slowly,…
DrOnline
  • 741
  • 1
  • 7
  • 9
48
votes
5 answers

Longest palindrome in a string using suffix tree

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…
shreyasva
  • 13,126
  • 25
  • 78
  • 101
43
votes
66 answers

How to write palindrome in JavaScript

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.
edgar7
  • 523
  • 2
  • 5
  • 7
38
votes
9 answers

how to find longest palindromic subsequence?

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…
user467871
29
votes
33 answers

Check if a string is a palindrome

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…
ankur
  • 4,565
  • 14
  • 64
  • 100
29
votes
50 answers

Palindrome Golf

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…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
23
votes
1 answer

How does this Java regex detect palindromes?

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"…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
22
votes
9 answers

Find all substrings that are palindromes

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;…
Himanshu Yadav
  • 13,315
  • 46
  • 162
  • 291
20
votes
2 answers

How does this PCRE pattern detect palindromes?

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…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
17
votes
10 answers

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

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…
Mead3000
  • 581
  • 2
  • 5
  • 18
16
votes
6 answers

longest palindromic substring recursive solution

I am aware of solutions that uses the bottom up dynamic programing approach to solve this problem in O(n^2). I am specifically looking for a top down dp approach. Is it possible to achieve longest palindromic substring using a recursive…
Trancey
  • 699
  • 1
  • 8
  • 18
1
2 3
99 100