diff --git a/README.md b/README.md index 28e182aa..6bc124c3 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/algorithms/string/palindrome-check/README.md b/src/algorithms/string/palindrome-check/README.md deleted file mode 100644 index 3d125744..00000000 --- a/src/algorithms/string/palindrome-check/README.md +++ /dev/null @@ -1,25 +0,0 @@ -# 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 deleted file mode 100644 index 4b227934..00000000 --- a/src/algorithms/string/palindrome-check/__test__/palindromeCheck.test.js +++ /dev/null @@ -1,14 +0,0 @@ -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/README.md b/src/algorithms/string/palindrome/README.md new file mode 100644 index 00000000..0ce77d42 --- /dev/null +++ b/src/algorithms/string/palindrome/README.md @@ -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/) diff --git a/src/algorithms/string/palindrome/__test__/isPalindrome.test.js b/src/algorithms/string/palindrome/__test__/isPalindrome.test.js new file mode 100644 index 00000000..0a49d5be --- /dev/null +++ b/src/algorithms/string/palindrome/__test__/isPalindrome.test.js @@ -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); + }); +}); diff --git a/src/algorithms/string/palindrome-check/palindromeCheck.js b/src/algorithms/string/palindrome/isPalindrome.js similarity index 83% rename from src/algorithms/string/palindrome-check/palindromeCheck.js rename to src/algorithms/string/palindrome/isPalindrome.js index c850fe3a..327fd6bb 100644 --- a/src/algorithms/string/palindrome-check/palindromeCheck.js +++ b/src/algorithms/string/palindrome/isPalindrome.js @@ -3,9 +3,10 @@ * @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; @@ -13,5 +14,6 @@ export default function palindromeCheck(string) { left += 1; right -= 1; } + return true; }