Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions valid-anagram/std-freejia.java
Copy link
Contributor

@hj4645 hj4645 Aug 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 문제는 맵을 하나만 선언해서 count를 빼주는 방식으로 풀었었는데, 2개를 선언해서 equals로 푸는 방법도 가독성 면에서 좋은 것 같습니다
2주차도 수고 많으셨습니다!😆
리뷰가 늦어 죄송합니다🥲

Map<Character, Integer> map = new HashMap<>();

        // s의 문자로 카운트 증가
        for (char c : s.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }

        // t의 문자로 카운트 감소
        for (char c : t.toCharArray()) {
            int count = map.getOrDefault(c, 0);
            if (count == 0) { // t에만 있는 문자가 있거나, 같은 문자가 더 많으면
                return false;
            }
            map.put(c, count - 1);
        }
        return true;

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;

Map<Character, Integer> mapS = new HashMap<>();
Map<Character, Integer> mapT = new HashMap<>();

for (int i = 0; i < s.length(); i++) {
mapS.put(s.charAt(i), mapS.getOrDefault(s.charAt(i), 0) + 1);
mapT.put(t.charAt(i), mapT.getOrDefault(t.charAt(i), 0) + 1);
}
return mapS.equals(mapT);
}
}