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 11class  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+ }
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments