Skip to content

Commit 5c7a0bd

Browse files
committed
valid-palindrome solution
1 parent 7a62e07 commit 5c7a0bd

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

number-of-1-bits/RiaOh.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var hammingWeight = function (n) {
6+
const binary = n.toString(2);
7+
const arr = [...binary];
8+
let count = 0;
9+
arr.map((num) => {
10+
if (num === "1") {
11+
count++;
12+
}
13+
});
14+
return count;
15+
};

valid-palindrome/RiaOh.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function (s) {
6+
const arr = [...s];
7+
let text = "";
8+
for (let i = 0; i < arr.length; i++) {
9+
if (isAlphabet(arr[i]) === true && arr[i] !== " ") {
10+
text = text + arr[i].toLowerCase();
11+
} else if (isNumeric(arr[i]) === true && arr[i] !== " ") {
12+
text = text + arr[i];
13+
}
14+
}
15+
console.log("text -->", text);
16+
for (let i = 0; i < text.length / 2; i++) {
17+
if (text[i] !== text[text.length - 1 - i]) {
18+
return false;
19+
}
20+
}
21+
return true;
22+
};
23+
24+
function isAlphabet(char) {
25+
return /[a-zA-Z]/.test(char);
26+
}
27+
28+
function isNumeric(str) {
29+
return !isNaN(str);
30+
}

0 commit comments

Comments
 (0)