题目描述:
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.
解题思路:
将字符串进行排序,然后逐个比较是否相同。
注意将数组转为String的时候需要使用String.valueOf()方法。不能用Array自带的toString()方法,详细原因见:http://www.cnblogs.com/ningvsban/p/3955483.html
代码如下:
public class Solution { public boolean isAnagram(String s, String t) { char[] s1 = s.toCharArray(); char[] t1 = t.toCharArray(); Arrays.sort(s1); Arrays.sort(t1); return String.valueOf(s1).equals(String.valueOf(t1)); } }