Skip to content

Commit ea28788

Browse files
liamlylehrtrekhleb
andauthored
added palindromeCheck (trekhleb#806)
* added readme * added readme * adding palindromeCheck * adjusted README Co-authored-by: Oleksii Trekhleb <trehleb@gmail.com>
1 parent b4b5110 commit ea28788

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ a set of rules that precisely define a sequence of operations.
102102
* `A` [Combination Sum](src/algorithms/sets/combination-sum) - find all combinations that form specific sum
103103
* **Strings**
104104
* `B` [Hamming Distance](src/algorithms/string/hamming-distance) - number of positions at which the symbols are different
105+
* `B` [Palindrome Check](src/algorithms/string/palindrome-check) - is the string the same in reverse
105106
* `A` [Levenshtein Distance](src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
106107
* `A` [Knuth–Morris–Pratt Algorithm](src/algorithms/string/knuth-morris-pratt) (KMP Algorithm) - substring search (pattern matching)
107108
* `A` [Z Algorithm](src/algorithms/string/z-algorithm) - substring search (pattern matching)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Palindrome Check
2+
3+
A Palindrome is a string that reads the same forwards and backwards.
4+
This means that the second half of the string is the reverse of the
5+
first half.
6+
7+
## Examples
8+
9+
The following are palindromes (thus would return TRUE):
10+
11+
- "a"
12+
- "pop" -> p + o + p
13+
- "deed" -> de + ed
14+
- "kayak" -> ka + y + ak
15+
- "racecar" -> rac + e + car
16+
17+
The following are NOT palindromes (thus would return FALSE):
18+
19+
- "rad"
20+
- "dodo"
21+
- "polo"
22+
23+
## References
24+
25+
[GeeksforGeeks - Check if a number is Palindrome](https://www.geeksforgeeks.org/check-if-a-number-is-palindrome/)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import palindromeCheck from '../palindromeCheck';
2+
3+
describe('palindromeCheck', () => {
4+
it('should return whether or not the string is a palindrome', () => {
5+
expect(palindromeCheck('a')).toBe(true);
6+
expect(palindromeCheck('pop')).toBe(true);
7+
expect(palindromeCheck('deed')).toBe(true);
8+
expect(palindromeCheck('kayak')).toBe(true);
9+
expect(palindromeCheck('racecar')).toBe(true);
10+
expect(palindromeCheck('rad')).toBe(false);
11+
expect(palindromeCheck('dodo')).toBe(false);
12+
expect(palindromeCheck('polo')).toBe(false);
13+
});
14+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string} string
3+
* @return {boolean}
4+
*/
5+
6+
export default function palindromeCheck(string) {
7+
let left = 0;
8+
let right = string.length - 1;
9+
while (left < right) {
10+
if (string[left] !== string[right]) {
11+
return false;
12+
}
13+
left += 1;
14+
right -= 1;
15+
}
16+
return true;
17+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy