From 66d4ae9a3fb1963be3e73e2fcc1e419f77ad59ba Mon Sep 17 00:00:00 2001 From: Onkar627 <74865775+Onkar627@users.noreply.github.com> Date: Wed, 6 Oct 2021 15:12:05 +0530 Subject: [PATCH] Add files via upload --- Programming/JavaScript/Check_Palindrome.js | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Programming/JavaScript/Check_Palindrome.js diff --git a/Programming/JavaScript/Check_Palindrome.js b/Programming/JavaScript/Check_Palindrome.js new file mode 100644 index 0000000..7a24bd4 --- /dev/null +++ b/Programming/JavaScript/Check_Palindrome.js @@ -0,0 +1,27 @@ +function isPalindrome(str) { + // Convert to lower case and replace + // any non - alpha or digit character with empty string. + str = str.toLowerCase().replace(/[^a-z\d]/g, ''); + + //Now by 2 pointer approach by making pointers at initial position and end position + // either they meet or pass each other. + for (let i = 0, j = str.length - 1; i < j; i++, j--) { + if (str[i] !== str[j]) return false; + } + + // If the loop completed with no issues, + // the string is a valid palindrome. + return true; +} + +//Example - String is 'race a car': +//Output : False + +//Example - String is 'madam'; +//Output : True + +//Time Complexity: +//Best ,Worst and Average complexity : O(n) + +//Space Complexity: +//Space Complexity of above program : O(1) \ No newline at end of file