Java simple solution using Sort


  • 0
    P
    public class Solution {
        public boolean isAnagram(String s, String t) {
            
            char[] sChars = s.toCharArray();
            Arrays.sort(sChars);
            String sStr = String.valueOf(sChars);
            
            char[] tChars = t.toCharArray();
            Arrays.sort(tChars);
            String tStr = String.valueOf(tChars);
            
            return sStr.equals(tStr);
        }
    }
    

    Time: O(nlogn)
    Space: O(n)


Log in to reply
 

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