LeetCode:Valid Anagram(java)

LeetCode:Valid Anagram(java)

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: s = "anagram", t = "nagaram" Output: true

Example 2:

Input: s = "rat", t = "car" Output: false

class Solution {
    public boolean isAnagram(String s, String t) {
        // convert s and t strings to arrays of characters
       char [] firstStr = s.toCharArray();
       char [] secondStr = t.toCharArray();

        // get array lenghts
        int l1 = firstStr.length;
        int l2 = secondStr.length;

        if(l1 != l2){
            return false;
        }
        // Sort Arrays
        Arrays.sort(firstStr);
        Arrays.sort(secondStr);


       return Arrays.equals(firstStr, secondStr);
}
    }