Skip to content

Commit 339aeaa

Browse files
authored
Merge pull request #2439 from junzero741/main
[junzero741] WEEK 03 Solutions
2 parents fddc0cc + 955cce8 commit 339aeaa

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

number-of-1-bits/junzero741.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// TC: O(log n)
2+
// SC: O(1)
3+
function hammingWeight(n: number): number {
4+
const binary = n.toString(2);
5+
let bits = 0;
6+
for(let i = 0; i < binary.length; i++) {
7+
if(binary[i] === '1') {
8+
bits++;
9+
}
10+
}
11+
12+
return bits;
13+
};

valid-palindrome/junzero741.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
function isPalindrome(s: string): boolean {
4+
5+
let L = 0;
6+
let R = s.length-1;
7+
8+
while(R > 0 && L < s.length - 1 && L <= R) {
9+
if(!isAlphanumeric(s[L])) {
10+
L++;
11+
continue;
12+
}
13+
if(!isAlphanumeric(s[R])) {
14+
R--;
15+
continue;
16+
}
17+
if(s[L].toLowerCase() !== s[R].toLowerCase()) {
18+
return false;
19+
}
20+
L++;
21+
R--;
22+
}
23+
24+
return true;
25+
};
26+
27+
function isAlphanumeric(str: string): boolean {
28+
return /^[a-zA-Z0-9]+$/.test(str);
29+
}

0 commit comments

Comments
 (0)