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
16
votes
10 answers

Palindrome detection efficiency

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…
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
15
votes
4 answers

Given a word, convert it into a palindrome with minimum addition of letters to it

Here is a pretty interesting interview question: Given a word, append the fewest number of letters to it to convert it into a palindrome. For example, if "hello" is the string given, the result should be "hellolleh." If "coco" is given, the…
brut3f0rc3
  • 1,173
  • 4
  • 13
  • 19
12
votes
2 answers

Finding the Longest Palindrome Subsequence with less memory

I am trying to solve a dynamic programming problem from Cormem's Introduction to Algorithms 3rd edition (pg 405) which asks the following: A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples…
11
votes
17 answers

Solving palindromic 'Triangle Quest' puzzle in Python

I'm trying to solve this programming puzzle: You are given a positive integer N (0 < N < 10). Your task is to print a palindromic triangle of size N. For example, a palindromic triangle of size 5 is: 1 121 12321 1234321 123454321 You can't take…
planetp
  • 14,248
  • 20
  • 86
  • 160
11
votes
9 answers

Longest palindrome in a string

I wrote the following function to find the longest palindrome in a string. It works fine but it won't work for words like "noon" or "redder". I fiddled around and changed the first line in the for loop from: var oddPal = centeredPalindrome(i,…
devdropper87
  • 4,025
  • 11
  • 44
  • 70
11
votes
2 answers

Proving that a reversible list is a palindrome in Coq

Here is my inductive definition of palindromes: Inductive pal { X : Type } : list X -> Prop := | pal0 : pal [] | pal1 : forall ( x : X ), pal [x] | pal2 : forall ( x : X ) ( l : list X ), pal l -> pal ( x :: l ++ [x] ). And the theorem I want…
user287393
  • 1,221
  • 8
  • 13
10
votes
10 answers

Recursive Function palindrome in Python

I need help writing a recursive function which detects whether a string is a palindrome. But i can't use any loops it must be recursive. Can anyone help show me how this is done . Im using Python.
Confused about Recursive
10
votes
3 answers

Finding palindrome using regex

This question comes in an attempt to understand one of the answer in : How to check that a string is a palindrome using regular expressions? Answer given by Markus Jarderot is : /^((.)(?1)\2|.?)$/ Can someone please explain, whats exactly happening…
NoobEditor
  • 15,563
  • 19
  • 81
  • 112
10
votes
5 answers

Convert string to palindrome string with minimum insertions

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) -…
whitepearl
  • 634
  • 2
  • 7
  • 16
9
votes
4 answers

Recursive Prolog predicate for reverse / palindrome

Can I get a recursive Prolog predicate having two arguments, called reverse, which returns the inverse of a list: Sample query and expected result: ?- reverse([a,b,c], L). L = [c,b,a]. A recursive Prolog predicate of two arguments called…
sam
  • 91
  • 1
  • 2
9
votes
4 answers

How to compute palindrome from a stream of characters in sub-linear space/time?

I don't even know if a solution exists or not. Here is the problem in detail. You are a program that is accepting an infinitely long stream of characters (for simplicity you can assume characters are either 1 or 0). At any point, I can stop the…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
9
votes
2 answers

Check if a list is a palindrome. If not, insert elements to make it a palindrome. (Prolog)

I have written the following code to check whether it is a palindrome or not. I have also created the logic to insert elements when the list is not a palindrome reverse_list(Inputlist, Outputlist) :- reverse(Inputlist, [], Outputlist). …
Pranav Kapoor
  • 1,171
  • 3
  • 13
  • 32
9
votes
8 answers

Check even/odd for Palindrome?

Is it a good idea to check for odd/even length of a palindrome number/string? Most snippets I came across don't do this basic test. If length is even, it can't be a palindrome, no? if len(var) % 2 != 0: # could be a palindrome, continue... else: …
eozzy
  • 66,048
  • 104
  • 272
  • 428
9
votes
1 answer

Finding Number of Palindromic Substrings in O(n) or O(n log n)?

I know you can find the longest palindromic substring in O(n) with manacher's algorithm, but is it possible to find the total number of palindromic substrings in O(n) or O(n log n)? If so, how would you go about doing it? Count single letters as…
1110101001
  • 4,662
  • 7
  • 26
  • 48
9
votes
9 answers

Palindromes using Scala

I came across this problem from CodeChef. The problem states the following: 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…
sc_ray
  • 7,803
  • 11
  • 63
  • 100