-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path125. Valid Palindrome.js
76 lines (64 loc) · 2.35 KB
/
125. Valid Palindrome.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
125. Valid Palindrome
https://leetcode.com/problems/valid-palindrome/
*/
/* TIME COMPLEXITY O(N) */
/*
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
*/
/**
* @param {string} s
* @return {boolean}
*/
//Create a function to check that the whole string is palindrom or not
//This function is the main function to check our string is valid palindrom or not
function check_palindrom(s) {
let pointer = s.length - 1; // Make a pointer which is start form length
for (let i = 0; i < s.length / 2; i++, pointer--) {
if (s[i] != s[pointer]) { // Check is the Ith possition value and pointer possition value is equal or not
return false; // return false if any of character is not equal we don't require to itterate whole string
}
}
return true; // Return true if all condition is satisfy
}
// Method 1
// This Below method is basically used for non-regex method.
var isPalindrome = function (s) {
// Make a array which is used to store all non-alphanumeric characters.
s = s.toLowerCase(); // Convert whole string to lower case.
var ansArray = [];
for (let i = 0; i < s.length; i++) {
// The bellow condition is used to check that the number should be a-z or 0-9
if ((s.charCodeAt(i) >= 97 && s.charCodeAt(i) <= 122) || (s.charCodeAt(i) >= 48 && (s.charCodeAt(i) <= 57))) {
ansArray.push(s[i]) // If the condition is true then add this value to the array
}
}
// Here The join function is used to convert array to string.
return check_palindrom(ansArray.join(""));
};
// Method 2
// Below method is basically used for use regex
var isPalindrome = function (s) {
// the below replace method is basically used to remove all non-alphanumeric character
// And the lower function is used for convert whole string to lower case
s = s.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
// Convert whole string to lower case.
// Here The join function is used to convert array to string.
return check_palindrom(s);
};
console.log(isPalindrome("0P"));
console.log(isPalindrome("A man, a plan, a canal: Panama"));
// char Code
// a = 97
// z = 122
// A = 65
// Z = 90
// 0 = 48
// 9 = 57