Java 8 simple solution


  • 0
    U
    public boolean isAnagram(String s, String t) {
            String s1 = toSortedCharStr(s);
            String t1 = toSortedCharStr(t);
            return s1.equals(t1);
        }
    
        private String toSortedCharStr(String s) {
            return s.chars()
                        .sorted()
                        .mapToObj(c -> (char) c)
                        .collect(Collectors.toList())
                        .toString();
        }
    

Log in to reply
 

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