From ea28788ed81541dc175d52c2986e9d55d2246dac Mon Sep 17 00:00:00 2001 From: liamlylehr Date: Sun, 23 Jan 2022 15:42:43 -0500 Subject: [PATCH] added palindromeCheck (#806) * added readme * added readme * adding palindromeCheck * adjusted README Co-authored-by: Oleksii Trekhleb --- README.md | 1 + .../string/palindrome-check/README.md | 25 +++++++++++++++++++ .../__test__/palindromeCheck.test.js | 14 +++++++++++ .../palindrome-check/palindromeCheck.js | 17 +++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 src/algorithms/string/palindrome-check/README.md create mode 100644 src/algorithms/string/palindrome-check/__test__/palindromeCheck.test.js create mode 100644 src/algorithms/string/palindrome-check/palindromeCheck.js diff --git a/README.md b/README.md index 7f0159c4..28e182aa 100644 --- a/README.md +++ b/README.md @@ -102,6 +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 * `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) diff --git a/src/algorithms/string/palindrome-check/README.md b/src/algorithms/string/palindrome-check/README.md new file mode 100644 index 00000000..3d125744 --- /dev/null +++ b/src/algorithms/string/palindrome-check/README.md @@ -0,0 +1,25 @@ +# Palindrome Check + +A 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/) diff --git a/src/algorithms/string/palindrome-check/__test__/palindromeCheck.test.js b/src/algorithms/string/palindrome-check/__test__/palindromeCheck.test.js new file mode 100644 index 00000000..4b227934 --- /dev/null +++ b/src/algorithms/string/palindrome-check/__test__/palindromeCheck.test.js @@ -0,0 +1,14 @@ +import palindromeCheck from '../palindromeCheck'; + +describe('palindromeCheck', () => { + it('should return whether or not the string is a palindrome', () => { + expect(palindromeCheck('a')).toBe(true); + expect(palindromeCheck('pop')).toBe(true); + expect(palindromeCheck('deed')).toBe(true); + expect(palindromeCheck('kayak')).toBe(true); + expect(palindromeCheck('racecar')).toBe(true); + expect(palindromeCheck('rad')).toBe(false); + expect(palindromeCheck('dodo')).toBe(false); + expect(palindromeCheck('polo')).toBe(false); + }); +}); diff --git a/src/algorithms/string/palindrome-check/palindromeCheck.js b/src/algorithms/string/palindrome-check/palindromeCheck.js new file mode 100644 index 00000000..c850fe3a --- /dev/null +++ b/src/algorithms/string/palindrome-check/palindromeCheck.js @@ -0,0 +1,17 @@ +/** + * @param {string} string + * @return {boolean} + */ + +export default function palindromeCheck(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; +}