Skip to content

Commit

Permalink
Merge pull request #345 from kim-young/main
Browse files Browse the repository at this point in the history
[kimyoung] WEEK 02 Solutions
  • Loading branch information
kim-young authored Aug 26, 2024
2 parents fcb4ee5 + 592782c commit d0eed34
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
24 changes: 24 additions & 0 deletions counting-bits/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var countBits = function (n) {
let result = [];
for (let i = 0; i <= n; i++) {
result.push(countOneBits(i)); // count one bits for each i until n
}

function countOneBits(num) { // method to count one bits
let oneBitCount = 0;
while (num) {
oneBitCount += num % 2;
num = num >> 1;
}
return oneBitCount;
}

return result;
};

// test cases
console.log(countBits(2)); // [0,1,1]
console.log(countBits(5)); // [0,1,1,2,1,2]

// time - O(nlogn)
// space - O(n)
47 changes: 47 additions & 0 deletions encode-and-decode-strings/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Encodes a list of strings to a single string.
*
* @param {string[]} strs
* @return {string}
*/

const DELIMITER = "#";

var encode = function (strs) {
let temp = "";
for (const str of strs) { // encode using the delimiter by attaching the length of the array as well
temp += `${str.length}${DELIMITER}` + str;
}
return temp;
};

/**
* Decodes a single string to a list of strings.
*
* @param {string} s
* @return {string[]}
*/
var decode = function (s) { // decode using the length of array that is attached with the delimiter
let result = [];
let i = 0;
while (i < s.length) {
let j = i;
while (s[j] !== DELIMITER) j++;
const len = Number(s.slice(i, j));
const word = s.slice(j + 1, j + len + 1);
result.push(word);
i = j + len + 1;
}
return result;
};

/**
* Your functions will be called as such:
* decode(encode(strs));
*/

// test cases
console.log(["Hello", "World"]);

// time - O(n) - iterate through the list of strings once
// space - O(n) - stores the strings
40 changes: 40 additions & 0 deletions valid-anagram/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// First Approach - hash map
var isAnagram = function (s, t) {
let map = {};
for (const char of s) {
// count character occurence of s
map[char] ? map[char]++ : (map[char] = 1);
}
for (const char of t) {
// compare character occurence of t to the map object
if (map[char]) {
map[char]--; // decrement each time
} else {
return false; // if there's a new character, return false
}
}
for (const el of Object.values(map)) {
// if all the values of the map object is 0, return true
if (el !== 0) return false; // otherwise return false;
}
return true;
};

// test cases
console.log(isAnagram("anagram", "nagarma"));
console.log(isAnagram("rat", "car"));

// time - O(s + t) - iterate through both input strings
// space - O(n) - map obj

//Second Approach - sorted strings
var isAnagram = function (s, t) {
return s.split("").sort().join("") === t.split("").sort().join("");
};

// test cases
console.log(isAnagram("anagram", "nagarma"));
console.log(isAnagram("rat", "car"));

// time - O(nlogn) - using sort method
// space - O(1) - no extra space memory

0 comments on commit d0eed34

Please sign in to comment.