Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 93d18b7

Browse files
author
rahulshishodia
authored
Update 242-Valid-Anagram.java
1 parent badc437 commit 93d18b7

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

java/242-Valid-Anagram.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
class Solution {
22
public boolean isAnagram(String s, String t) {
3-
if (s.length() != t.length()) return false;
43

5-
int[] countS = new int[26];
6-
int[] countT = new int[26];
4+
if(s.length() != t.length()) return false;
5+
if(s.equals(t)) return true;
76

8-
for (int i = 0 ; i < s.length() ; i++) {
9-
countS[s.charAt(i) - 'a']++;
10-
countT[t.charAt(i) - 'a']++;
11-
}
7+
Map<Character, Integer> sMap = new HashMap<>();
8+
Map<Character, Integer> tMap = new HashMap<>();
129

13-
for (int i = 0 ; i < 26 ; i++) {
14-
if (countS[i] - countT[i] != 0) return false;
10+
for(int i = 0; i < s.length(); i++) {
11+
sMap.merge(s.charAt(i), 1, Integer::sum);
12+
tMap.merge(t.charAt(i), 1, Integer::sum);
1513
}
1614

15+
for(Character c : sMap.keySet()) {
16+
if(!sMap.get(c).equals(tMap.get(c))) return false;
17+
}
1718
return true;
1819
}
19-
}
20+
}

0 commit comments

Comments
 (0)