From de2dbacb7dd23d9adefceaac585f09a59e2da1c5 Mon Sep 17 00:00:00 2001 From: "green.carlos" Date: Sat, 28 Feb 2026 09:26:39 -0800 Subject: [PATCH 1/3] Sorting and HashSet solutions for 1.1 isUnique --- chapter01/1.1 - Is Unique/isUnique.js | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/chapter01/1.1 - Is Unique/isUnique.js b/chapter01/1.1 - Is Unique/isUnique.js index 3321571..ff435be 100644 --- a/chapter01/1.1 - Is Unique/isUnique.js +++ b/chapter01/1.1 - Is Unique/isUnique.js @@ -35,6 +35,43 @@ function everyCharUnique(str) { return true; } +/** + Sorting solution + Time O(n log n) + Space O(n) [for recursion] + + We can also sort the input string as an array + Then iterate over the input string + If we find a duplicate character return false + + Otherwise if we get through the entire string return true + + **/ + +function everyCharUnique(string) { + const str = string.split('').sort() + for (let i = 1; i < str.length; i++) { + if (str[i] === str[i - 1]) { + return false + } + } + return true +} + +/** + HashSet Solution + Time & Space O(n) + + The Hashing function can also be reduced to two lines + Utilizing a HashSet function, we only keep track of unique elements + Then compare the length / size of both string & set respectively + **/ +function everyCharUnique(string) { + const set = new Set(string.split('')) + return string.length === set.size +} + + /* TESTS */ console.log(everyCharUnique('abcd'), 'true'); console.log(everyCharUnique('abccd'), 'false'); From f3d0806b9a498d3f8f249915f74262ca181aa8f2 Mon Sep 17 00:00:00 2001 From: "green.carlos" Date: Sun, 1 Mar 2026 13:02:58 -0800 Subject: [PATCH 2/3] 1.2 Check Permutation Time & Space O(n) with hashTable --- chapter01/1.2 - Check Perm/checkPermute.js | 45 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/chapter01/1.2 - Check Perm/checkPermute.js b/chapter01/1.2 - Check Perm/checkPermute.js index 0cb0931..6f12957 100644 --- a/chapter01/1.2 - Check Perm/checkPermute.js +++ b/chapter01/1.2 - Check Perm/checkPermute.js @@ -1,3 +1,5 @@ +// Time O(n log n) for Sorting +// Space O(n) for sorting var checkPermute = function(stringOne, stringTwo) { // if different lengths, return false if (stringOne.length !== stringTwo.length) { @@ -11,9 +13,48 @@ var checkPermute = function(stringOne, stringTwo) { } }; +/** +* @param: {string} str1 +* @param: {string} str2 +* @return: {boolean} + +Time O(n) & Space O(n) + +Iterate over the initial input string +Throw all of the elements into a frequency counter +Iterate over the second input string + +Then subtract 1 from each element +Iterate over the hashTable and make sure all of the values are zero + +**/ + +const checkPermute = function(str1, str2) { + const freq = str1.split('').reduce((a,c) => { + a[c] = (a[c] || 0 ) + 1 + return a + }, {}) + + for (let chr of str2) { + freq[chr]-- + } + + for (let key in freq) { + if (freq[key] !== 0) { + return false + } + } + + return true +} + // Tests console.log(checkPermute('aba', 'aab'), true); - console.log(checkPermute('aba', 'aaba'), false); +console.log(checkPermute('aba', 'aa'), false); -console.log(checkPermute('aba', 'aa'), false); \ No newline at end of file +console.log(checkPermute('', ''), true) +console.log(checkPermute('abc', 'cba'), true) +console.log(checkPermute('', 'abc'), false) +console.log(checkPermute('aaaaaaaaaaaaaaaaaa', 'aaaaaaaaaaaaaaaaaa'), true) +console.log(checkPermute('aaaaaaa1aaaaaaaaaa', 'aaaaaaaaaaaaaaaaaa'), false) From 56f46949c3a7e6f75ed2a89cb52c73a47e5ea982 Mon Sep 17 00:00:00 2001 From: "green.carlos" Date: Sun, 1 Mar 2026 16:16:22 -0800 Subject: [PATCH 3/3] HashTable solution --- chapter01/1.1 - Is Unique/isUnique.js | 37 --------------------------- 1 file changed, 37 deletions(-) diff --git a/chapter01/1.1 - Is Unique/isUnique.js b/chapter01/1.1 - Is Unique/isUnique.js index ff435be..3321571 100644 --- a/chapter01/1.1 - Is Unique/isUnique.js +++ b/chapter01/1.1 - Is Unique/isUnique.js @@ -35,43 +35,6 @@ function everyCharUnique(str) { return true; } -/** - Sorting solution - Time O(n log n) - Space O(n) [for recursion] - - We can also sort the input string as an array - Then iterate over the input string - If we find a duplicate character return false - - Otherwise if we get through the entire string return true - - **/ - -function everyCharUnique(string) { - const str = string.split('').sort() - for (let i = 1; i < str.length; i++) { - if (str[i] === str[i - 1]) { - return false - } - } - return true -} - -/** - HashSet Solution - Time & Space O(n) - - The Hashing function can also be reduced to two lines - Utilizing a HashSet function, we only keep track of unique elements - Then compare the length / size of both string & set respectively - **/ -function everyCharUnique(string) { - const set = new Set(string.split('')) - return string.length === set.size -} - - /* TESTS */ console.log(everyCharUnique('abcd'), 'true'); console.log(everyCharUnique('abccd'), 'false');