palindrome

Missing return statement error in Java

不羁岁月 提交于 2019-12-17 21:28:31
问题 I am currently writing a palindrome tester in Java for a class I am taking in high school. I have asked my teacher for assistance and he is also confused as well. I was hoping the community on stackoverflow could help me out. Thank you. public class Palindrome { private String sentence; public Palindrome(String s) { sentence = s; } public boolean isPalindrome() { if(sentence.length() <= 1) { return true; } if(sentence.charAt(0) == sentence.charAt(sentence.length()-1)) { sentence = sentence

Recursive Prolog predicate for reverse / palindrome

我的未来我决定 提交于 2019-12-17 13:55:44
问题 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 palindrome which returns true if the given list is palindrome. Sample query with expected result: ?- palindrome([a,b,c]). false. ?- palindrome([b,a,c,a,b]). true. 回答1: Ad 1: It is impossible to define reverse/2 as a ( directly edit thx to @repeat: tail)

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

断了今生、忘了曾经 提交于 2019-12-17 02:04:12
问题 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 can get substring of every possible combination from the array Have a palindrome function which checks if a string is palindrome so for every substring (i,j) call this function, if it is a palindrome store it in a string variable If you find next

How to understand this C++ palindrome code?

倖福魔咒の 提交于 2019-12-14 03:32:50
问题 I recently came to the solution of the palindrome problem, but I do not understand how this part of code works (with rbegin and rend). Can someone explain it to me? #include <iostream> #include <string> using namespace std; bool checkPalindrome(string); int main() { string inputString = "palindrom"; cout << checkPalindrome(inputString); return 0; } bool checkPalindrome(std::string inputString) { return (inputString == string(inputString.rbegin(), inputString.rend())); } 回答1: Taking a look at

Palindrome in Java with NUmbers [closed]

ぃ、小莉子 提交于 2019-12-13 22:40:51
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I am self studying java, i am on while loops already, I have an exercise here regarding a palindrome. what's a palindrome? how will code it? any ideas? or pseudocode for it? I am really confused here NOT a HOMEWORK 回答1: Since its not homework, it shouldn't matter how you implement

How do you determine if a String is a palindrome? [duplicate]

╄→гoц情女王★ 提交于 2019-12-13 10:12:06
问题 This question already has answers here : Check string for palindrome (36 answers) Closed 4 years ago . How do you test if a given String is a palindrome in Java, without using any methods that do it all for me? 回答1: String palindrome = "..." // from elsewhere boolean isPalindrome = palindrome.equals(new StringBuilder(palindrome).reverse().toString()); 回答2: public boolean checkPalindrome(string word){ for(int i=0 ; i < word.length()/2;i++) { if(word.charAt(i) ! = word.charAt(word.length()-1-i)

How to check if string is palindrome WITHOUT using string functions in C++ [closed]

你说的曾经没有我的故事 提交于 2019-12-13 09:56:06
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I am tasked to create a program that will check if the entered string is a palindrome or not, so I have this simple program here: #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; int main() { char y[100], x[100]; cout << "Enter word" << endl; cin >>

Convert string to palindrome with fewest number of insertions

落花浮王杯 提交于 2019-12-12 18:20:19
问题 This is a question from https://www.dailycodingproblem.com/: Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible anywhere in the word. If there is more than one palindrome of minimum length that can be made, return the lexicographically earliest one (the first one alphabetically). For example, given the string "race", you should return "ecarace", since we can add three letters to it (which is the smallest amount to make a palindrome).

Add the least amount of characters to make a palindrome

给你一囗甜甜゛ 提交于 2019-12-12 07:58:31
问题 The question: Given any string, add the least amount of characters possible to make it a palindrome in linear time. I'm only able to come up with a O(N 2 ) solution. Can someone help me with an O(N) solution? 回答1: Revert the string Use a modified Knuth-Morris-Pratt to find the latest match (simplest modification would be to just append the original string to the reverted string and ignore matches after len(string). Append the unmatched rest of the reverted string to the original. 1 and 3 are

Java Palindrome checker

大兔子大兔子 提交于 2019-12-12 03:29:43
问题 I have a issue with my palindrome checker as it is not working properly. It must be able to test these 4 things: TestPalindrome("Madam, I'm Adam", true); TestPalindrome("addbda", false ); TestPalindrome("", false); TestPalindrome("Dammit, I'm mad", true); This is my code for the palindrome: public static boolean IsPalindrome( String inputString ) { String reverse = ""; for(int i = inputString.replaceAll("[^a-zA-Z ]", "").length() -1; i>=0; i--){ reverse = reverse + inputString.replaceAll("[^a