【LeetCode】242 Valid Anagram

Valid Anagram 
Total Accepted: 4232 Total Submissions: 11283 My Submissions Question Solution 
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.
【解题思路】
判断两个字符串是不是由颠倒字母顺序而构成的同一个单词。

Java AC

public class Solution {
    public boolean isAnagram(String s, String t) {
        if (s == null && t == null){
            return true;
        }
        if (s == null || t == null){
            return false;
        }
        int sLen = s.length();
        int tLen = t.length();
        if (sLen != tLen){
            return false;
        }
        char sArray[] = s.toCharArray();
        char tArray[] = t.toCharArray();
        Arrays.sort(sArray);
        Arrays.sort(tArray);
        int k = 0;
        while (k < sLen){
            if (sArray[k] != tArray[k]){
                return false;
            }
            k++;
        }
        return true;
    }
}


你可能感兴趣的:(【LeetCode】242 Valid Anagram)