leetcode 242: Valid Anagram

Valid Anagram

Total Accepted: 2864 Total Submissions: 7420

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.

[思路]

sort 以后看是否相等

思路2: bitmap, 数字符个数是否相等.

[CODE]

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


你可能感兴趣的:(leetcode 242: Valid Anagram)