File tree Expand file tree Collapse file tree 1 file changed +11
-10
lines changed Expand file tree Collapse file tree 1 file changed +11
-10
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public boolean isAnagram (String s , String t ) {
3
- if (s .length () != t .length ()) return false ;
4
3
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 ;
7
6
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 <>();
12
9
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 );
15
13
}
16
14
15
+ for (Character c : sMap .keySet ()) {
16
+ if (!sMap .get (c ).equals (tMap .get (c ))) return false ;
17
+ }
17
18
return true ;
18
19
}
19
- }
20
+ }
You can’t perform that action at this time.
0 commit comments