Skip to content

Commit ba72383

Browse files
committed
feat: valid anagram
1 parent 5439322 commit ba72383

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

valid-anagram/minji-go.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Problem: https://leetcode.com/problems/valid-anagram/
3+
Description: return true if one string is an anagram of the other, one formed by rearranging the letters of the other
4+
Concept:String, Hash Table, Sorting, Array, Counting, String Matching, Ordered Map, Ordered Set, Hash Function ...
5+
Time Complexity: O(n), Runtime: 27ms
6+
Space Complexity: O(n), Memory: 43.11MB
7+
*/
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
class Solution {
12+
public boolean isAnagram(String s, String t) {
13+
if(s.length() != t.length()) return false;
14+
15+
Map<Character, Integer> count = new HashMap<>();
16+
for(int i=0; i<s.length(); i++){
17+
count.put(s.charAt(i), count.getOrDefault(s.charAt(i), 0)+1);
18+
count.put(t.charAt(i), count.getOrDefault(t.charAt(i), 0)-1);
19+
}
20+
for(Character key : count.keySet()){
21+
if(count.get(key)!=0) return false;
22+
}
23+
return true;
24+
}
25+
}

0 commit comments

Comments
 (0)