From 829fce8f11c5e06f7979618db758962e970e5e65 Mon Sep 17 00:00:00 2001 From: Elias Afara Date: Sun, 15 Oct 2023 23:50:10 +0200 Subject: [PATCH] add: isPalindrome function implementation --- DIRECTORY.md | 3 +++ Strings/PalindromeCheck.js | 25 +++++++++++++++++++++ Strings/tests/PalindromeCheck.test.js | 31 +++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 Strings/PalindromeCheck.js create mode 100644 Strings/tests/PalindromeCheck.test.js diff --git a/DIRECTORY.md b/DIRECTORY.md index 5d14e1d..13607c7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -20,3 +20,6 @@ - [QuickSort](Sorting/QuickSort.js) - [SelectionSort](Sorting/SelectionSort.js) - [ThreeNumberSort](Sorting/ThreeNumberSort.js) + +- **Strings** + - [PalindromeCheck](Strings/PalindromeCheck.js) diff --git a/Strings/PalindromeCheck.js b/Strings/PalindromeCheck.js new file mode 100644 index 0000000..275b544 --- /dev/null +++ b/Strings/PalindromeCheck.js @@ -0,0 +1,25 @@ +/** + * Check if a given string is a palindrome. + * + * @param {string} str - The input string to be checked for palindrome. + * @returns {boolean} - True if the input string is a palindrome, false otherwise. + * + * @description + * A palindrome is a string that reads the same forwards and backwards. This function + * checks whether the input string is a palindrome, ignoring spaces, punctuation, and + * letter casing. It handles single-character strings as palindromes as well. + * + * @complexity + * - Time Complexity: O(n), where n is the length of the input string. + * The function iterates through the string once to check for palindromic properties. + * - Space Complexity: O(1), as the function only uses a constant amount of extra memory. + */ +function isPalindrome(str) { + // Remove non-alphanumeric characters and convert to lowercase + str = str.replace(/[^a-zA-Z0-9]/g, "").toLowerCase(); + + // Check if the cleaned string is the same when reversed + return str === str.split("").reverse().join(""); +} + +export { isPalindrome }; diff --git a/Strings/tests/PalindromeCheck.test.js b/Strings/tests/PalindromeCheck.test.js new file mode 100644 index 0000000..1b93b24 --- /dev/null +++ b/Strings/tests/PalindromeCheck.test.js @@ -0,0 +1,31 @@ +import { isPalindrome } from "../PalindromeCheck"; + +describe("isPalindrome", () => { + it("returns true for a valid palindrome", () => { + expect(isPalindrome("racecar")).toBe(true); + }); + + it("returns true for a single character", () => { + expect(isPalindrome("a")).toBe(true); + }); + + it("returns true for an empty string", () => { + expect(isPalindrome("")).toBe(true); + }); + + it("returns false for a non-palindrome", () => { + expect(isPalindrome("hello")).toBe(false); + }); + + it("ignores spaces and punctuation", () => { + expect(isPalindrome("A man, a plan, a canal, Panama")).toBe(true); + }); + + it("handles letter casing", () => { + expect(isPalindrome("RacECar")).toBe(true); + }); + + it("returns false for a non-palindrome with spaces", () => { + expect(isPalindrome("not a palindrome")).toBe(false); + }); +});