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 ...
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-...
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 ...
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 ...
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:
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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:
...
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:
...
8
votes
Palindrome checking function
To start with id remove the code that you written twice by re-arranging the if statement into this
...
8
votes
Python palindrome program
You don't need the loop. Once i > 0, the if statement will always be False:
...
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 ...
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 ...
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 ...
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 ...
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.
...
7
votes
The Next Palindromic number
#include <string.h>
If you want to use C++ string facilities, using <string>. The library ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
palindrome × 261python × 76
programming-challenge × 76
java × 53
algorithm × 52
strings × 50
performance × 46
c++ × 42
beginner × 36
c × 24
python-3.x × 21
time-limit-exceeded × 21
javascript × 18
c# × 17
interview-questions × 17
c++11 × 8
ruby × 7
complexity × 7
dynamic-programming × 7
haskell × 6
primes × 6
python-2.x × 5
recursion × 5
linked-list × 5
array × 4