From db842387ac14f3c0821cd5fedf26af773cae9882 Mon Sep 17 00:00:00 2001 From: mykoo Date: Thu, 22 Aug 2024 18:02:27 +0900 Subject: [PATCH 1/3] valid-anagram --- valid-anagram/GUMUNYEONG.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 valid-anagram/GUMUNYEONG.js diff --git a/valid-anagram/GUMUNYEONG.js b/valid-anagram/GUMUNYEONG.js new file mode 100644 index 000000000..337b4ee39 --- /dev/null +++ b/valid-anagram/GUMUNYEONG.js @@ -0,0 +1,26 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function (s, t) { + const countHash = {}; + let result = true; + + if (s.length !== t.length) return false; + + for (str_t of t) { + countHash[str_t] ? countHash[str_t]++ : countHash[str_t] = 1; + } + + for (str_s of s) { + if (countHash[str_s]) { + countHash[str_s]--; + } else { + result = false; + break; + } + } + + return result; +}; From 265046d23cd98b1bdfa42a6abe3b571ed7de12cb Mon Sep 17 00:00:00 2001 From: mykoo Date: Sun, 25 Aug 2024 01:59:48 +0900 Subject: [PATCH 2/3] =?UTF-8?q?valid-anagram=20=EB=B3=B5=EC=9E=A1=EB=8F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valid-anagram/GUMUNYEONG.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/valid-anagram/GUMUNYEONG.js b/valid-anagram/GUMUNYEONG.js index 337b4ee39..0ecef53b6 100644 --- a/valid-anagram/GUMUNYEONG.js +++ b/valid-anagram/GUMUNYEONG.js @@ -24,3 +24,9 @@ var isAnagram = function (s, t) { return result; }; + +// TC : O(n) +// n(=s의 길이 = t의 길이) 만큼 반복 하므로 On(n) + +// SC : O(n) +// 최대크기 n(=s의 길이 = t의 길이)만큼인 객체를 생성하므로 공간 복잡도도 O(n) From c690fd8ae2d5b9013ca7d393a8fc083cfac0f23c Mon Sep 17 00:00:00 2001 From: GUMUNYEONG Date: Sun, 25 Aug 2024 11:53:13 +0900 Subject: [PATCH 3/3] MOD : valid-anagram --- valid-anagram/GUMUNYEONG.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/valid-anagram/GUMUNYEONG.js b/valid-anagram/GUMUNYEONG.js index 0ecef53b6..fdec3080c 100644 --- a/valid-anagram/GUMUNYEONG.js +++ b/valid-anagram/GUMUNYEONG.js @@ -5,7 +5,6 @@ */ var isAnagram = function (s, t) { const countHash = {}; - let result = true; if (s.length !== t.length) return false; @@ -17,12 +16,11 @@ var isAnagram = function (s, t) { if (countHash[str_s]) { countHash[str_s]--; } else { - result = false; - break; + return false; } } - return result; + return true; }; // TC : O(n)