Skip to content

Commit

Permalink
feat: solve valid anagram
Browse files Browse the repository at this point in the history
  • Loading branch information
GangBean committed Dec 15, 2024
1 parent b58d47d commit bd7b3c2
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions valid-anagram/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public boolean isAnagram(String s, String t) {
/**
1. ๋ฌธ์ œ ์ดํ•ด.
- ์•„๋‚˜๊ทธ๋žจ: ๋ฌธ์ž์˜ ์ˆœ์„œ๋งŒ ๋ฐ”๋€Œ์—ˆ์„ ๋•Œ ๋™์ผํ•œ ๊ธ€์ž
- ๊ตฌ์„ฑ ๋ฌธ์ž์™€ ์ˆซ์ž๋งŒ ๋™์ผํ•˜๋ฉด ์•„๋‚˜๊ทธ๋žจ
2. ํ’€์ด ๋ฐฉ์‹
- comapre s.length() with t.length()
- loop over s and t, count each word's character and it's count, and then save it as map(key-value pair structure)
- compare map of s and t
3. Complexity
- time complexity: O(N)
- space complexity: O(N)
*/

if (s.length() != s.length()) return false;

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

for (char c: s.toCharArray()) {
sMap.put(c, sMap.getOrDefault(c, 0) + 1);
}
for (char c: t.toCharArray()) {
tMap.put(c, tMap.getOrDefault(c, 0) + 1);
}

return Objects.equals(sMap, tMap);
}
}

0 comments on commit bd7b3c2

Please sign in to comment.