-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eunhwa99
committed
Dec 15, 2024
1 parent
4c8241e
commit 7d30552
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
|
||
// s ์์ ๋ฌธ์๋ค์ด t ์๋ ๋์ผํ ํ์๋ก ๋ฑ์ฅํ๋ ์ง ํ์ธ | ||
if (s.length() != t.length()) return false; // ๋ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ ๋ค๋ฅด๋ค๋ฉด ์๋๊ทธ๋จ์ด ์๋๋ค. | ||
|
||
// ๋ฌธ์๋ณ ํ์ ์ ์ฅ map | ||
Map<Character, Integer> sAlphabetCountMap = new HashMap<>(); | ||
for (char c : s.toCharArray()) { // ์๊ฐ๋ณต์ก๋: O(n) | ||
sAlphabetCountMap.put(c, sAlphabetCountMap.getOrDefault(c, 0) + 1); | ||
} | ||
|
||
for (char c : t.toCharArray()) { // ์๊ฐ๋ณต์ก๋: O(n) | ||
if (!sAlphabetCountMap.containsKey(c)) return false; // s์ t๊ฐ ๊ฐ์ง ๋ฌธ์์ด์ด ์๋ค๋ฉด ์๋๊ทธ๋จ์ด ์๋๋ค. | ||
|
||
int count = sAlphabetCountMap.get(c) - 1; | ||
if (count == 0) sAlphabetCountMap.remove(c); | ||
else sAlphabetCountMap.put(c, count); | ||
} | ||
|
||
// ๋ชจ๋ ๋ฌธ์๊ฐ ์ผ์นํ๋ฉด ํด์๋งต์ด ๋น์ด ์์ด์ผ ํจ | ||
return sAlphabetCountMap.isEmpty(); | ||
} | ||
} |