Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kimyoung] WEEK 02 Solutions #345

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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("");
haklee marked this conversation as resolved.
Show resolved Hide resolved
};

// 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