From 59e26f05c11e87aa2f99603c8445447592a5eb5d Mon Sep 17 00:00:00 2001 From: Jaehyeon Robert Han Date: Wed, 18 Dec 2024 15:12:50 -0800 Subject: [PATCH 1/5] valid-anagram solution --- valid-anagram/Zioq.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 valid-anagram/Zioq.js diff --git a/valid-anagram/Zioq.js b/valid-anagram/Zioq.js new file mode 100644 index 000000000..e1d0611e1 --- /dev/null +++ b/valid-anagram/Zioq.js @@ -0,0 +1,29 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + if( s.length !== t.length ) {return false} + + // Change string to arr + let s_arr = s.split(''); + let t_arr = t.split(''); + + let s_arr_sort = s_arr.sort(); + let t_arr_sort = t_arr.sort(); + + return JSON.stringify(s_arr_sort) === JSON.stringify(t_arr_sort); // Comparison array +}; +/* + Time Complexity: O(n log n) (dominated by sorting). + Space Complexity: O(n) (dominated by the arrays and string representations). + +*/ + + + + + +// console.log(isAnagram("anagram","nagaram")); +// console.log(isAnagram("rat","car")); \ No newline at end of file From a1ac4ea282005e325edaf219b16165c746230c3a Mon Sep 17 00:00:00 2001 From: Jaehyeon Robert Han Date: Wed, 18 Dec 2024 15:22:25 -0800 Subject: [PATCH 2/5] add lines --- valid-anagram/Zioq.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/valid-anagram/Zioq.js b/valid-anagram/Zioq.js index e1d0611e1..d6ae75a15 100644 --- a/valid-anagram/Zioq.js +++ b/valid-anagram/Zioq.js @@ -22,8 +22,6 @@ var isAnagram = function(s, t) { */ - - - // console.log(isAnagram("anagram","nagaram")); -// console.log(isAnagram("rat","car")); \ No newline at end of file +// console.log(isAnagram("rat","car")); + From c6d5b719947498e7c0f42b99b5731195e324414f Mon Sep 17 00:00:00 2001 From: Jaehyeon Robert Han Date: Wed, 18 Dec 2024 16:21:17 -0800 Subject: [PATCH 3/5] new solution --- valid-anagram/Zioq.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/valid-anagram/Zioq.js b/valid-anagram/Zioq.js index d6ae75a15..a1aa75989 100644 --- a/valid-anagram/Zioq.js +++ b/valid-anagram/Zioq.js @@ -7,21 +7,36 @@ var isAnagram = function(s, t) { if( s.length !== t.length ) {return false} // Change string to arr - let s_arr = s.split(''); - let t_arr = t.split(''); + // let s_arr = s.split(''); + // let t_arr = t.split(''); + /* WHAT IF SORT IS NOT ALLOWED */ + // let s_arr_sort = s_arr.sort(); + // let t_arr_sort = t_arr.sort(); + // return JSON.stringify(s_arr_sort) === JSON.stringify(t_arr_sort); // Comparison array - let s_arr_sort = s_arr.sort(); - let t_arr_sort = t_arr.sort(); - return JSON.stringify(s_arr_sort) === JSON.stringify(t_arr_sort); // Comparison array + // Use map to count characters in the s + // And remove character with t from map + + const map = new Map(); + + for (let c of s) { + map[c] = (map[c] || 0) + 1; + } + + for (let c of t) { + if (!map[c]) return false; + map[c]--; + } + + return true; }; /* - Time Complexity: O(n log n) (dominated by sorting). - Space Complexity: O(n) (dominated by the arrays and string representations). - + Time Complexity: O(n) + Space Complexity: O(n) */ -// console.log(isAnagram("anagram","nagaram")); -// console.log(isAnagram("rat","car")); +console.log(isAnagram("anagram","nagaram")); +console.log(isAnagram("rat","car")); From 7c4fa157789e656303fff05ecf878dc11832a3b0 Mon Sep 17 00:00:00 2001 From: Jaehyeon Robert Han Date: Thu, 19 Dec 2024 22:08:24 -0800 Subject: [PATCH 4/5] limbing-stairs solution --- climbing-stairs/Zioq.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 climbing-stairs/Zioq.js diff --git a/climbing-stairs/Zioq.js b/climbing-stairs/Zioq.js new file mode 100644 index 000000000..4b15be44d --- /dev/null +++ b/climbing-stairs/Zioq.js @@ -0,0 +1,29 @@ +/** + * @param {number} n + * @return {number} + */ +var climbStairs = function(n) { + // 1 -> 1 + // 2 -> (1+1) | 2 + // 3 -> (1+1+1) | (1+2) | (2+1) + // 4 -> (1+1+1+1) | (2+1+1) | (1+2+1) | (1+1+2) | (1+2+2) + // 5 -> (1+1+1+1+1) | (2+1+1+1) | (1+2+1+1) | (1+1+2+1) | (1+1+1+2) | (1+2+2) | (2+1+2) | (2+2+1) + if(n <=3) return n + + let a = 0; + let b = 1; + let c = 0; + + for(let i=0; i Date: Thu, 19 Dec 2024 22:08:46 -0800 Subject: [PATCH 5/5] add single line --- climbing-stairs/Zioq.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/climbing-stairs/Zioq.js b/climbing-stairs/Zioq.js index 4b15be44d..e199bb1b7 100644 --- a/climbing-stairs/Zioq.js +++ b/climbing-stairs/Zioq.js @@ -26,4 +26,5 @@ var climbStairs = function(n) { console.log(climbStairs(2)) console.log(climbStairs(3)) console.log(climbStairs(4)) -console.log(climbStairs(5)) \ No newline at end of file +console.log(climbStairs(5)) +