Skip to content

Commit bd7b3c2

Browse files
committed
feat: solve valid anagram
1 parent b58d47d commit bd7b3c2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

β€Žvalid-anagram/GangBean.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public boolean isAnagram(String s, String t) {
3+
/**
4+
1. 문제 이해.
5+
- μ•„λ‚˜κ·Έλž¨: 문자의 μˆœμ„œλ§Œ λ°”λ€Œμ—ˆμ„ λ•Œ λ™μΌν•œ κΈ€μž
6+
- ꡬ성 λ¬Έμžμ™€ 숫자만 λ™μΌν•˜λ©΄ μ•„λ‚˜κ·Έλž¨
7+
2. 풀이 방식
8+
- comapre s.length() with t.length()
9+
- loop over s and t, count each word's character and it's count, and then save it as map(key-value pair structure)
10+
- compare map of s and t
11+
3. Complexity
12+
- time complexity: O(N)
13+
- space complexity: O(N)
14+
*/
15+
16+
if (s.length() != s.length()) return false;
17+
18+
Map<Character, Integer> sMap = new HashMap<>();
19+
Map<Character, Integer> tMap = new HashMap<>();
20+
21+
for (char c: s.toCharArray()) {
22+
sMap.put(c, sMap.getOrDefault(c, 0) + 1);
23+
}
24+
for (char c: t.toCharArray()) {
25+
tMap.put(c, tMap.getOrDefault(c, 0) + 1);
26+
}
27+
28+
return Objects.equals(sMap, tMap);
29+
}
30+
}
31+

0 commit comments

Comments
Β (0)