Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions number-of-1-bits/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function hammingWeight(n: number): number {
// 1. convert the number to a binary string
const binaryString = n.toString(2);

// 2. count the number of 1s in the binary string
return binaryString.split("").filter((char) => char === "1").length;
}
30 changes: 30 additions & 0 deletions valid-palindrome/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function isPalindrome(s: string): boolean {
// 1. filter out non-alphanumeric characters
const validChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnlopqrstuvwxyz0123456789";

// 2. compare the first and last characters
let i = 0;
let j = s.length - 1;

while (i < j) {
const head = s[i];
const tail = s[j];

if (!validChars.includes(head)) {
// 3. if the characters are not alphanumeric, move the pointer inward
i++;
} else if (!validChars.includes(tail)) {
// 3. if the characters are not alphanumeric, move the pointer inward
j--;
} else if (head.toLowerCase() !== tail.toLowerCase()) {
// 4. if the characters are not the same, return false
return false;
} else {
// 5. if the characters are the same, move the pointers inward
i++;
j--;
}
}
return true;
}