JAVA----Easiest to understand


  • 0
    H

    The basic idea here is to convert two strings to char array, and we are going to sort the arrays.
    Compare two arrays by using a for loop,if the characters are not the same at i, return false.

    public boolean isAnagram(String s, String t) {
        if(s.length()!=t.length()){
            return false;
        }
        char[] sc = s.toCharArray();
        char[] tc = t.toCharArray();
        Arrays.sort(sc);
        Arrays.sort(tc);
        for(int i=0;i<sc.length;i++){
            if(sc[i]!=tc[i]){
                return false;
            }
        }
        return true;
    }
    

    I know it is not the optimal solution and I am willing to modify it. Fight for the interview!


Log in to reply
 

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