LeetCode:Valid Anagram

问题描述:

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

Note:
You may assume the string contains only lowercase alphabets.

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?

思路:

对字母进行排序比较

JAVA代码:

public class Solution {
    public boolean isAnagram(String s, String t) {
        char [] sarray = s.toCharArray();
        char [] tarray = t.toCharArray();
        Arrays.sort(sarray);
        Arrays.sort(tarray);
        return String.valueOf(sarray).equals(String.valueOf(tarray));
        
    }
}


你可能感兴趣的:(LeetCode)