-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 13 ms (32.07%), Space: 44.8 MB (13.33%) - LeetHub
- Loading branch information
1 parent
0702e19
commit 98216e0
Showing
1 changed file
with
7 additions
and
9 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 |
---|---|---|
@@ -1,19 +1,17 @@ | ||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
if (s.length() != t.length()) return false; | ||
HashMap<Character, Integer> map = new HashMap<>(26); | ||
HashMap<Character, Integer> map = new HashMap<>(); | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1); | ||
for (Character ch : s.toCharArray()) { | ||
map.put(ch, map.getOrDefault(ch, 0) + 1); | ||
} | ||
|
||
for (int i = 0; i < t.length(); i++) { | ||
if (map.containsKey(t.charAt(i)) && (map.get(t.charAt(i)) > 1)) map.put(t.charAt(i), map.get(t.charAt(i)) - 1); | ||
else if (map.containsKey(t.charAt(i))) map.remove(t.charAt(i)); | ||
else return false; | ||
for (Character ch : t.toCharArray()) { | ||
if (!map.containsKey(ch) || map.get(ch) == 0) return false; | ||
map.put(ch, map.get(ch) - 1); | ||
} | ||
|
||
if (map.isEmpty()) return true; | ||
return false; | ||
return true; | ||
} | ||
} |