palindrome

Finding the Longest Palindrome Subsequence with less memory

ε祈祈猫儿з 提交于 2019-12-02 19:35:49
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 algorithm should return carac . Well, I could solve it in two ways: First solution: The Longest

[leetcode] Palindrome Number

邮差的信 提交于 2019-12-02 19:12:31
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case? There is a more generic way of solving this problem. 思路1(不推荐):利用Reverse Integer的方法,求的转换后的数字,然后比较是否相等。提示说这样有溢出的问题,想了想感觉问题不大,leetcode也过了。因为首先输入必须是一个合法的int值,负数直接返回false,对于正数

highest palindrome with 3 digit numbers in python

故事扮演 提交于 2019-12-02 18:37:21
In problem 4 from http://projecteuler.net/ it says: 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. I have this code here def isPalindrome(num): return str(num) == str(num)[::-1] def largest(bot, top): for x in range(top, bot, -1): for y in range(top,bot, -1): if isPalindrome(x*y): return x*y print largest(100,999) It should find the largest palindrome, it spits out 580085 which I believe to be correct, but project euler doesn't think

Why my code for checking if a number is a palindrom won't work?

别等时光非礼了梦想. 提交于 2019-12-02 12:58:12
My Java code is here: import java.util.Scanner; public class task2 { public static void main(String args[]) { System.out.print("Input a 3 digit int"); Scanner scan = new Scanner(System.in); int x = scan.nextInt(); int isPalindrome = 0; while (x != 0) { isPalindrome = isPalindrome*10 + x % 10; x /= 10; } { if (x == isPalindrome){ System.out.print ("Yes, this is a palindrome!"); } else { System.out.print("No, try again"); } } } } The code will only recognize a palindrome if the numbers entered are zeroes. I'm having trouble understanding why. This is because the value of x is getting changed

how to count the number of words of the same(PALINDROME) in java

北城余情 提交于 2019-12-02 11:14:16
I'm a newbie Java Developer. I want to write code to count the number of palindrome words in the paragraph using Java. The assumptions are : User can enter a paragraph containing as many sentences as possible. Each word is separated by a whitespace, and each sentence is separated by a period and The punctuation right before or after the word will be ignored, while the punctuation inside the word will be counted. Sample Input : Otto goes to school. Otto sees a lot of animals at the pets store. Sample output : Otto = 2 a = 1 Sees = 1 Read the file into your program, split the entries at every

Simple Palindrome Detector [duplicate]

℡╲_俬逩灬. 提交于 2019-12-02 10:14:58
This question already has an answer here: Check string for palindrome 36 answers 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 start = in.nextLine(); String end=start; boolean isPalindrome = (start == end); if (isPalindrome) { System.out.print("This is a

BASH Palindrome Checker

丶灬走出姿态 提交于 2019-12-02 07:54:24
问题 This is my first time posting on here so bear with me please. I received a bash assignment but my professor is completely unhelpful and so are his notes. Our assignment is to filter and print out palindromes from a file. In this case, the directory is: /usr/share/dict/words The word lengths range from 3 to 45 and are supposed to only filter lowercase letters (the dictionary given has characters and uppercases, as well as lowercase letters). i.e. "-dkas-das" so something like "q-evvavve-q" may

BASH Palindrome Checker

纵饮孤独 提交于 2019-12-02 07:14:17
This is my first time posting on here so bear with me please. I received a bash assignment but my professor is completely unhelpful and so are his notes. Our assignment is to filter and print out palindromes from a file. In this case, the directory is: /usr/share/dict/words The word lengths range from 3 to 45 and are supposed to only filter lowercase letters (the dictionary given has characters and uppercases, as well as lowercase letters). i.e. "-dkas-das" so something like "q-evvavve-q" may count as a palindrome but i shouldn't be getting that as a proper result. Anyways, I can get it to

Can I make a code in python that ignores special characters such as commas, spaces, exclamation points, etc?

狂风中的少年 提交于 2019-12-02 05:29:59
I want to create a code that will return "true" (if I type in a palindrome regardless of case or if there are special characters in it), and "false" otherwise. The code I have so far works for phrases with no special characters such as commas, apostrophes, spaces, etc. def is_palindrome(my_str): my_str= my_str.casefold() rev_str= reversed(my_str) if list(my_str) == list(rev_str): print("True") else: print("False") when I do: print (is_palindrome("Rats live on no evil star")) it returns True because it is a palindrome when I do: print (is_palindrome("Hello World!")) it returns False because it

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

▼魔方 西西 提交于 2019-12-01 06:48:13
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 plan, a canal. Panama" and "never odd or even" return false, meaning somewhere has to be a mistake. You