Skip to main content
36 votes
Accepted

Palindrome checking function

Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word ...
Scott Sauyet's user avatar
22 votes

Tests for palindromes in C and C++

A special case length < 3 seems like a bug. A string aa is a palindrome, and one could successfully argue that a single-...
vnp's user avatar
  • 58.7k
17 votes
Accepted

Determine if a word is a palindrome

Here are some observations that may help you improve your code. Don't abuse using namespace std Putting using namespace std at ...
Edward's user avatar
  • 67.2k
16 votes
Accepted

Tests for palindromes in C and C++

There are some things to be said about your C version as well, but since you explicitly asked about the C++ version (and also because my C-knowledge is not that great), I will leave those for somebody ...
Ben Steffan's user avatar
  • 5,333
13 votes

Tests for palindromes in C and C++

I've seen a lot of answers here over-generalizing. You're dealing with a std::string, so you can just do: ...
David's user avatar
  • 1,058
13 votes
Accepted

Palindrome checker program

To answer your comment, yes there are equivalent of PEP8 in java. I suggest checkstyle, this plugin works in lots of IDE / text editors and can be configured with ...
Doi9t's user avatar
  • 3,374
12 votes
Accepted

Check if a given linked list is palindrome

More tests Having a test suite is nice. It would be even nicer to make it more comprehensive by adding: edge cases (empty list for instance) inputs with an even number of elements negative test cases ...
SylvainD's user avatar
  • 29.8k
12 votes
Accepted

Easy to read palindrome checker

Time complexity Your time complexity is linear but you can save a few traversals over the string and lower the constant factor as you improve readability. Checking whether a string is a palindrome ...
ggorlen's user avatar
  • 4,167
11 votes

Determining if a palindrome exists in a linked list using recursion

Generally, when you end up with an expression like this, it is a sign that something is not right, and something needs rethinking or refactoring. I don't see the point of ...
Bex's user avatar
  • 251
11 votes
Accepted

Find longest Palindrome substring

The code is reasonably clear and obvious, but has some severe inefficiencies. First, let's pick up some simple oversights. Both isPalindrome() and ...
Toby Speight's user avatar
  • 88.3k
11 votes

Palindrome test

While it is not quite definitive, it looks like you use using namespace std;. That namespace is not designed for wholesale inclusion, being vast and subject to ...
Deduplicator's user avatar
  • 19.9k
10 votes

Palindrome checker program

In java is the preferred way of curly brace placing on the same line like you can see in code snippets in Oracles Style Guide. reverse_string has a type embedded in ...
Roman's user avatar
  • 2,923
9 votes

The Next Palindromic number

Well, my first impression is that your formatting is broken. Generally, before you even consider checking in a new revision to your local source-control or showing it to anyone, the code should be ...
Deduplicator's user avatar
  • 19.9k
9 votes

Python palindrome program

I would expect a function that has a "predicate" name (is_x()) to return a boolean value rather than a string. The boolean is much more useful to the ...
Toby Speight's user avatar
  • 88.3k
9 votes

Palindrome checker program

Doi9t's answer is very good, but even with their improvements, there is still a problem: your code does not produce the correct answer in all cases. Java strings use UTF-16 encoding. This means that a ...
MJ713's user avatar
  • 736
9 votes
Accepted

Python palindrome checker function

Localisation It's fine to localise your user-facing strings (e.g. print('Die Buchstaben in diesem Wort bilden ein Palindrom'), but it is not advisable to write the ...
Reinderien's user avatar
  • 71.1k
9 votes

Determine if a word is a palindrome

I'm a newbie programmer and was given a task to find if the input word is a palindrome or not. I hope that means that you're taking lessons, and this is an assignment. I think you're at a ...
JDługosz's user avatar
  • 11.7k
9 votes
Accepted

Checking whether a string is a permutation of a palindrome in C++20

While your algorithm is \$O(n)\$, the constant factors are considerable. Specifically, you are suffering due to all the nodes the map needs. Better options are: Just ...
Deduplicator's user avatar
  • 19.9k
8 votes
Accepted

Determining if a palindrome exists in a linked list using recursion

Since the first two statements are mutually exclusive, you can write it as: ...
Daniel's user avatar
  • 4,632
8 votes
Accepted

C++ check if Palindrome

Advice 1: getting the job done the C++ way You can check whether the input string is a palindrome in one line: ...
coderodde's user avatar
  • 31.9k
8 votes

Palindrome checking function

To start with id remove the code that you written twice by re-arranging the if statement into this ...
Dan's user avatar
  • 530
8 votes

Python palindrome program

You don't need the loop. Once i > 0, the if statement will always be False: ...
C.Nivs's user avatar
  • 3,117
8 votes

Checking whether a string is a permutation of a palindrome in C++20

Using a std::unordered_map is overkill here, since the number of possible characters is known beforehand. So you could just use a ...
G. Sliepen's user avatar
  • 69.2k
8 votes
Accepted

Euler - Largest Palindrome Product in Java

Avoid keeping State in a static variable / Minimize the scope of variable The advantage of not doing so might not be immediately obvious in this case because it's a sort of one-shot code (in a sense ...
Alexander Ivanchenko's user avatar
7 votes

Counting the number of continuous palindromic substrings

First off, you should organize your code by using functions as it will help you simplify it and is better for reusability and testing. As I see it, you can use a function to generate the various ...
301_Moved_Permanently's user avatar
7 votes
Accepted

Counting the number of continuous palindromic substrings

Style Python has a Style Guide called PEP 8. It is definitly worth reading and trying to apply. You'll find various tools to help you if needed. The main issues are: missing whitespace indentation ...
SylvainD's user avatar
  • 29.8k
7 votes
Accepted

Palindrome algorithm

As already stated you don't need to set it to true multiple times. There is cleaner syntax for this. ...
paparazzo's user avatar
  • 6,136
7 votes

The Next Palindromic number

#include <string.h> If you want to use C++ string facilities, using <string>. The library ...
Snowhawk's user avatar
  • 6,770
7 votes

CTCI Chapter 1 : Palindrome Permutation

Nice implementation. Here are a couple suggestions collections.defaultdict Intead of hashTable[i] = hashTable.get(i, 0) + 1, use ...
RootTwo's user avatar
  • 10.7k

Only top scored, non community-wiki answers of a minimum length are eligible