diff --git a/counting-bits/kimyoung.js b/counting-bits/kimyoung.js new file mode 100644 index 000000000..5e6838579 --- /dev/null +++ b/counting-bits/kimyoung.js @@ -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) diff --git a/encode-and-decode-strings/kimyoung.js b/encode-and-decode-strings/kimyoung.js new file mode 100644 index 000000000..06cf2aab8 --- /dev/null +++ b/encode-and-decode-strings/kimyoung.js @@ -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 diff --git a/valid-anagram/kimyoung.js b/valid-anagram/kimyoung.js new file mode 100644 index 000000000..2d9f5c47a --- /dev/null +++ b/valid-anagram/kimyoung.js @@ -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