Skip to content

Commit

Permalink
Time: 13 ms (32.07%), Space: 44.8 MB (13.33%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-S-Sahu committed Oct 23, 2024
1 parent 0702e19 commit 98216e0
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions 0242-valid-anagram/0242-valid-anagram.java
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;
}
}

0 comments on commit 98216e0

Please sign in to comment.