Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

added palindrome checker Java #505

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Programming/JavaScript/Check_Palindrome.js
Original file line number Diff line number Diff line change
@@ -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)