My easy java solution based on sorting


  • 0
    Z
    public boolean isAnagram(String s, String t) {
            if( s== null || t == null || ( s.length() == 0 && t.length() == 0) ){
                return true;
            }
            
            if( s.length() != t.length() ) {
                return false;
            }
            
            String code1 = encode(s);
            String code2 = encode(t);
            
            return code1.equals(code2);
            
        }
        
        private String encode(String s) {
            char[] array = s.toCharArray();
            Arrays.sort( array );
            return new String( array );
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.