Skip to content

Commit d0eed34

Browse files
authored
Merge pull request #345 from kim-young/main
[kimyoung] WEEK 02 Solutions
2 parents fcb4ee5 + 592782c commit d0eed34

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

counting-bits/kimyoung.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var countBits = function (n) {
2+
let result = [];
3+
for (let i = 0; i <= n; i++) {
4+
result.push(countOneBits(i)); // count one bits for each i until n
5+
}
6+
7+
function countOneBits(num) { // method to count one bits
8+
let oneBitCount = 0;
9+
while (num) {
10+
oneBitCount += num % 2;
11+
num = num >> 1;
12+
}
13+
return oneBitCount;
14+
}
15+
16+
return result;
17+
};
18+
19+
// test cases
20+
console.log(countBits(2)); // [0,1,1]
21+
console.log(countBits(5)); // [0,1,1,2,1,2]
22+
23+
// time - O(nlogn)
24+
// space - O(n)

encode-and-decode-strings/kimyoung.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Encodes a list of strings to a single string.
3+
*
4+
* @param {string[]} strs
5+
* @return {string}
6+
*/
7+
8+
const DELIMITER = "#";
9+
10+
var encode = function (strs) {
11+
let temp = "";
12+
for (const str of strs) { // encode using the delimiter by attaching the length of the array as well
13+
temp += `${str.length}${DELIMITER}` + str;
14+
}
15+
return temp;
16+
};
17+
18+
/**
19+
* Decodes a single string to a list of strings.
20+
*
21+
* @param {string} s
22+
* @return {string[]}
23+
*/
24+
var decode = function (s) { // decode using the length of array that is attached with the delimiter
25+
let result = [];
26+
let i = 0;
27+
while (i < s.length) {
28+
let j = i;
29+
while (s[j] !== DELIMITER) j++;
30+
const len = Number(s.slice(i, j));
31+
const word = s.slice(j + 1, j + len + 1);
32+
result.push(word);
33+
i = j + len + 1;
34+
}
35+
return result;
36+
};
37+
38+
/**
39+
* Your functions will be called as such:
40+
* decode(encode(strs));
41+
*/
42+
43+
// test cases
44+
console.log(["Hello", "World"]);
45+
46+
// time - O(n) - iterate through the list of strings once
47+
// space - O(n) - stores the strings

valid-anagram/kimyoung.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// First Approach - hash map
2+
var isAnagram = function (s, t) {
3+
let map = {};
4+
for (const char of s) {
5+
// count character occurence of s
6+
map[char] ? map[char]++ : (map[char] = 1);
7+
}
8+
for (const char of t) {
9+
// compare character occurence of t to the map object
10+
if (map[char]) {
11+
map[char]--; // decrement each time
12+
} else {
13+
return false; // if there's a new character, return false
14+
}
15+
}
16+
for (const el of Object.values(map)) {
17+
// if all the values of the map object is 0, return true
18+
if (el !== 0) return false; // otherwise return false;
19+
}
20+
return true;
21+
};
22+
23+
// test cases
24+
console.log(isAnagram("anagram", "nagarma"));
25+
console.log(isAnagram("rat", "car"));
26+
27+
// time - O(s + t) - iterate through both input strings
28+
// space - O(n) - map obj
29+
30+
//Second Approach - sorted strings
31+
var isAnagram = function (s, t) {
32+
return s.split("").sort().join("") === t.split("").sort().join("");
33+
};
34+
35+
// test cases
36+
console.log(isAnagram("anagram", "nagarma"));
37+
console.log(isAnagram("rat", "car"));
38+
39+
// time - O(nlogn) - using sort method
40+
// space - O(1) - no extra space memory

0 commit comments

Comments
 (0)