palindrome

Why will this recursive regex only match when a character repeats 2^n - 1 times?

自闭症网瘾萝莉.ら 提交于 2020-01-11 02:32:07
问题 After reading polygenelubricants's series of articles on advanced regular expressions techniques (particularly How does this Java regex detect palindromes?), I decided to attempt to create my own PCRE regex to parse a palindrome, using recursion (in PHP). What I came up with was: ^(([a-z])(?1)\2|[a-z]?)$ My understanding of this expression is that it should either match zero or one characters (every string of less than 2 characters is implicitly a palindrome, as well as to account for

How can we make maximum number of palindromic substrings by rearranging the characters in a string?

久未见 提交于 2020-01-06 14:41:34
问题 A string is called a substring of another string, if it can be obtained from that string by dropping some (possibly zero) number of characters from the beginning and from the end of it. For example, abc , ab , and c are substrings of the string abc , while ac and d are not. Let's define a palindromic count of the string as the number of its substrings that are palindromes. For example, the palindromic count of the string aaa is 6, because all of its substrings are palindromes. And the

C program to find if a number is palindrome or not

不想你离开。 提交于 2020-01-05 09:35:20
问题 I made a C program to check if a number is palindrome or not. I used the following code, but it shows numbers like 12321 as non palindrome. Can you please explain me the mistake in the program below? #include <stdio.h> int main() { int i, x, n, c, j; int d=0; printf ("enter total digits in number: "); scanf ("%d", &i); printf ("\nenter number: "); scanf ("%d", &n); j=n; for (x=1; x<=i; x++) { c= j%10; d=c*(10^(i-x))+d; j=(j-c)/10; } if (d==n) { printf ("\npalindrome"); } else { printf ("\nnon

Longest palindromic substring and suffix trie

本小妞迷上赌 提交于 2020-01-02 12:46:54
问题 I was Googling about a rather well-known problem, namely: the longest palindromic substring I have found links that recommend suffix tries as a good solution to the problem. Example SO and Algos The approach is (as I understand it) e.g. for a string S create Sr (which is S reversed) and then create a generalized suffix trie. Then find the longest common sustring of S and Sr which is the path from the root to the deepest node that belongs both to S and Sr . So the solution using the suffix

Longest palindromic substring and suffix trie

℡╲_俬逩灬. 提交于 2020-01-02 12:46:30
问题 I was Googling about a rather well-known problem, namely: the longest palindromic substring I have found links that recommend suffix tries as a good solution to the problem. Example SO and Algos The approach is (as I understand it) e.g. for a string S create Sr (which is S reversed) and then create a generalized suffix trie. Then find the longest common sustring of S and Sr which is the path from the root to the deepest node that belongs both to S and Sr . So the solution using the suffix

Finding the Longest Palindrome Subsequence with less memory

倖福魔咒の 提交于 2019-12-31 09:04:56
问题 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 of palindromes are all strings of length 1, civic , racecar , and aibohphobia (fear of palindromes). Give an efficient algorithm to find the longest palindrome that is a subsequence of a given input string. For example, given the input character , your

Python palindrome program not working

放肆的年华 提交于 2019-12-31 07:00:49
问题 I've written a simple program in python which checks if the sentence is palindrome. But I can't figure out why isn't it working. The results is always False. Does anyone knows what's wrong? def isPalindrome(word): # Removes all spaces, and lowercase the word. word = word.strip().lower() word = word.replace(" ", "") # If the length of the word is less than 1, means its a palindrome if (len(word) <= 1): return True # Compares the first and the last character of the word. # If it is the same,

Simple Palindrome Detector [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 05:14:09
问题 This question already has answers here : Check string for palindrome (36 answers) Closed last year . I'm working on a palindrome detector. I've already managed to make it work in some cases. When I type in a word like "Abba" it comes back as a palindrome. Great! But it doesn't do the same thing if I enter "cat". I can't use loops for this. Any ideas? public class Palindrome { public static void main(String[]args) { Scanner in=new Scanner(System.in); System.out.print("Enter word"); String

Palindrome Checker in JavaScript - don't know how to debug

耗尽温柔 提交于 2019-12-30 10:32:15
问题 I want to build a palindrome checker in javascript. All non-letter characters should be removed, so that a phrase like "A man, a plan, a canal. Panama" can also be a palindrome. function reverse(str) { return str.split("").reverse().join(""); } function palindrome(str) { str = str.replace(/[^a-zA-Z]+/,"").toLowerCase(); if(str == reverse(str)) { return true; } else { return false; } } Now, where is the mistake in the above lines? The code works on some examples. But for instance "A man, a

Finding palindrome using regex

て烟熏妆下的殇ゞ 提交于 2019-12-30 08:18:09
问题 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 here....i need to do similar in Perl , but not able to understand this solution!!! PS : I am not very good in perl so please go easy ....and also "this can't be considered a regular expression if you want to be strict" - i read this line, so i am