From 588b03e9ac26c908fed6f738289280c122bc9ce3 Mon Sep 17 00:00:00 2001 From: Nour B <56294154+nourrrrrrrr@users.noreply.github.com> Date: Fri, 1 May 2020 13:08:49 +0100 Subject: [PATCH 1/2] Create palindrome algorithm --- maths/Palindrome.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 maths/Palindrome.js diff --git a/maths/Palindrome.js b/maths/Palindrome.js new file mode 100644 index 0000000000..438b02329e --- /dev/null +++ b/maths/Palindrome.js @@ -0,0 +1,45 @@ +/** + * A palindrome is any string that can be reversed and still be the same. + * An example of one is 'radar', since it is spelled the same even after + * being reversed. One method to check if a + * + * Here's how this works recursively: + * + * Palindrome('radar') + * true && Palindrome('ada') + * true && true && Palindrome('d') + * true && true && true && true + * + * @flow + * @complexity: O(n) + */ +export default function PalindromeRecursive(string: string): boolean { + // Base case + if (string.length < 2) return true; + + // Check outermost keys + if (string[0] !== string[string.length - 1]) { + return false; + } + + return PalindromeRecursive(string.slice(1, string.length - 1)); +} + +export function PalindromeIterative(string: string): boolean { + const _string = string + .toLowerCase() + .replace(/ /g, '') + .replace(/,/g, '') + .replace(/'.'/g, '') + .replace(/:/g, '') + .split(''); + + // A word of only 1 character is already a palindrome, so we skip to check it + while (_string.length > 1) { + if (_string.shift() !== _string.pop()) { + return false; + } + } + + return true; +} From c124a0cdec3629bbcab8c097309d5cd32987a985 Mon Sep 17 00:00:00 2001 From: vinayak Date: Wed, 6 May 2020 14:49:47 +0530 Subject: [PATCH 2/2] Update Palindrome.js --- maths/Palindrome.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/maths/Palindrome.js b/maths/Palindrome.js index 438b02329e..4abc8d9976 100644 --- a/maths/Palindrome.js +++ b/maths/Palindrome.js @@ -13,33 +13,39 @@ * @flow * @complexity: O(n) */ -export default function PalindromeRecursive(string: string): boolean { + +function PalindromeRecursive (string) { // Base case - if (string.length < 2) return true; + if (string.length < 2) return true // Check outermost keys if (string[0] !== string[string.length - 1]) { - return false; + return false } - return PalindromeRecursive(string.slice(1, string.length - 1)); + return PalindromeRecursive(string.slice(1, string.length - 1)) } -export function PalindromeIterative(string: string): boolean { +function PalindromeIterative (string) { const _string = string .toLowerCase() .replace(/ /g, '') .replace(/,/g, '') .replace(/'.'/g, '') .replace(/:/g, '') - .split(''); + .split('') // A word of only 1 character is already a palindrome, so we skip to check it while (_string.length > 1) { if (_string.shift() !== _string.pop()) { - return false; + return false } } - return true; + return true } + +// testing + +console.log(PalindromeRecursive('Javascript Community')) +console.log(PalindromeIterative('mom'))