From 8a86dd8c7efc1764c0ea44083e7a3e53cfff95b2 Mon Sep 17 00:00:00 2001 From: forest000014 Date: Mon, 16 Dec 2024 16:47:18 +0900 Subject: [PATCH 1/3] Valid Anagram --- valid-anagram/forest000014.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 valid-anagram/forest000014.java diff --git a/valid-anagram/forest000014.java b/valid-anagram/forest000014.java new file mode 100644 index 000000000..09e20eb5a --- /dev/null +++ b/valid-anagram/forest000014.java @@ -0,0 +1,32 @@ +/* +s와 t는 알파벳 소문자로만 이루어지므로, 카운팅을 위해 26개의 고정된 key를 사용하면 충분하고, 배열이 가장 간단하고 적합하다고 생각함 + +Runtime: 4 ms (Beats: 76.59%) +Time Complexity: O(n) + +Memory: 43.04 MB (Beats: 78.65%) +Space Complexity: O(1) +*/ + +class Solution { + public boolean isAnagram(String s, String t) { + if (s.length() != t.length()) + return false; + + int[] cnt = new int[26]; + + for (int i = 0; i < s.length(); i++) { + cnt[s.charAt(i) - 'a']++; + } + for (int i = 0; i < t.length(); i++) { + cnt[t.charAt(i) - 'a']--; + } + + for (int i = 0; i < 26; i++) { + if (cnt[i] != 0) + return false; + } + + return true; + } +} From 1754c1dd36d02470444288ff5c1c1a49a09c0d9e Mon Sep 17 00:00:00 2001 From: forest000014 Date: Mon, 16 Dec 2024 16:48:04 +0900 Subject: [PATCH 2/3] Climbing Stairs --- climbing-stairs/forest000014.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 climbing-stairs/forest000014.java diff --git a/climbing-stairs/forest000014.java b/climbing-stairs/forest000014.java new file mode 100644 index 000000000..07170a04e --- /dev/null +++ b/climbing-stairs/forest000014.java @@ -0,0 +1,30 @@ +/* +i번째 칸에 가는 방법은 (1) i-2번째 칸에서 2칸을 점프하거나 (2) i-1번째 칸에서 1칸을 점프하는 2가지 방법 뿐입니다. (MECE함) +따라서, (i번째 칸에 가는 경우의 수) = (i-2번째 칸에 가는 경우의 수) + (i-1번째 칸에 가는 경우의 수) + +Runtime: 0 ms (Beats: 100.00%) +Time Complexity: O(n) + +Memory: 40.47 MB (Beats: 36.79%) +Space Complexity: O(1) +*/ + +class Solution { + public int climbStairs(int n) { + if (n == 1) { + return 1; + } else if (n == 2) { + return 2; + } else { + int prev2 = 1; + int prev1 = 2; + int cur = 0; + for (int i = 3; i <= n; i++) { + cur = prev2 + prev1; + prev2 = prev1; + prev1 = cur; + } + return cur; + } + } +} \ No newline at end of file From 0bd7993b6a6ef0a267dce1b1f1e01906ac73a54b Mon Sep 17 00:00:00 2001 From: forest000014 Date: Mon, 16 Dec 2024 16:50:26 +0900 Subject: [PATCH 3/3] =?UTF-8?q?Climbing=20Stairs=20-=20=EB=A7=88=EC=A7=80?= =?UTF-8?q?=EB=A7=89=20=EA=B0=9C=ED=96=89=20=EB=AC=B8=EC=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- climbing-stairs/forest000014.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/climbing-stairs/forest000014.java b/climbing-stairs/forest000014.java index 07170a04e..52e93cfb4 100644 --- a/climbing-stairs/forest000014.java +++ b/climbing-stairs/forest000014.java @@ -27,4 +27,4 @@ public int climbStairs(int n) { return cur; } } -} \ No newline at end of file +}