From 9a14e75976e0872f30f8065ffa21ad6e5d028849 Mon Sep 17 00:00:00 2001 From: Young Kim Date: Sun, 18 Aug 2024 21:10:36 -0400 Subject: [PATCH 1/3] Valid Anagram solution --- valid-anagram/kimyoung.js | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 valid-anagram/kimyoung.js 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 From 9c1a79b1468a5abfda1820b2f213fb80085a7994 Mon Sep 17 00:00:00 2001 From: Young Kim Date: Sun, 18 Aug 2024 21:27:24 -0400 Subject: [PATCH 2/3] Counting Bits solution --- counting-bits/kimyoung.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 counting-bits/kimyoung.js 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) From 592782c6504b0bb22cc24cce6046d4986076ffab Mon Sep 17 00:00:00 2001 From: Young Kim Date: Mon, 19 Aug 2024 23:59:25 -0400 Subject: [PATCH 3/3] Encode and Decode Strings solution --- encode-and-decode-strings/kimyoung.js | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 encode-and-decode-strings/kimyoung.js 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