Skip to content

Commit

Permalink
Code/README fixes for the "Palindrome Check".
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Jan 23, 2022
1 parent ea28788 commit 90addf9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ a set of rules that precisely define a sequence of operations.
* `A` [Combination Sum](src/algorithms/sets/combination-sum) - find all combinations that form specific sum
* **Strings**
* `B` [Hamming Distance](src/algorithms/string/hamming-distance) - number of positions at which the symbols are different
* `B` [Palindrome Check](src/algorithms/string/palindrome-check) - is the string the same in reverse
* `B` [Palindrome](src/algorithms/string/palindrome) - check if the string is the same in reverse
* `A` [Levenshtein Distance](src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
* `A` [Knuth–Morris–Pratt Algorithm](src/algorithms/string/knuth-morris-pratt) (KMP Algorithm) - substring search (pattern matching)
* `A` [Z Algorithm](src/algorithms/string/z-algorithm) - substring search (pattern matching)
Expand Down
25 changes: 0 additions & 25 deletions src/algorithms/string/palindrome-check/README.md

This file was deleted.

This file was deleted.

29 changes: 29 additions & 0 deletions src/algorithms/string/palindrome/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Palindrome Check

A [Palindrome](https://en.wikipedia.org/wiki/Palindrome) is a string that reads the same forwards and backwards.
This means that the second half of the string is the reverse of the
first half.

## Examples

The following are palindromes (thus would return `TRUE`):

```
- "a"
- "pop" -> p + o + p
- "deed" -> de + ed
- "kayak" -> ka + y + ak
- "racecar" -> rac + e + car
```

The following are NOT palindromes (thus would return `FALSE`):

```
- "rad"
- "dodo"
- "polo"
```

## References

- [GeeksForGeeks - Check if a number is Palindrome](https://www.geeksforgeeks.org/check-if-a-number-is-palindrome/)
15 changes: 15 additions & 0 deletions src/algorithms/string/palindrome/__test__/isPalindrome.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import isPalindrome from '../isPalindrome';

describe('palindromeCheck', () => {
it('should return whether or not the string is a palindrome', () => {
expect(isPalindrome('a')).toBe(true);
expect(isPalindrome('pop')).toBe(true);
expect(isPalindrome('deed')).toBe(true);
expect(isPalindrome('kayak')).toBe(true);
expect(isPalindrome('racecar')).toBe(true);

expect(isPalindrome('rad')).toBe(false);
expect(isPalindrome('dodo')).toBe(false);
expect(isPalindrome('polo')).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
* @return {boolean}
*/

export default function palindromeCheck(string) {
export default function isPalindrome(string) {
let left = 0;
let right = string.length - 1;

while (left < right) {
if (string[left] !== string[right]) {
return false;
}
left += 1;
right -= 1;
}

return true;
}

0 comments on commit 90addf9

Please sign in to comment.