# 242. Valid Anagram 有效的字母异位词

@TOC

[LeetCode]

Total Accepted: 78186 Total Submissions: 187211 Difficulty: Easy

## # 题目描述

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

Example 1:

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

Example 2:

``````Input: s = "rat", t = "car"
Output: 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?

## # 题目大意

### # 字典统计词频

``````public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int[] sTimes=new int[26];
int[] tTimes=new int[26];
for(int i=0;i<s.length();i++){
sTimes[s.charAt(i)-'a']+=1;
tTimes[t.charAt(i)-'a']+=1;
}
for(int i=0;i<sTimes.length;i++){
if(sTimes[i]!=tTimes[i]){
return false;
}
}
return true;
}
}
``````

AC:12ms

``````public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int[] times=new int[26];
for(int i=0;i<s.length();i++){
times[s.charAt(i)-'a']+=1;
times[t.charAt(i)-'a']-=1;
}
for(int i=0;i<times.length;i++){
if(times[i]!=0){
return false;
}
}
return true;
}
}
``````

AC:10ms

``````class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
scount = collections.Counter(s)
tcount = collections.Counter(t)
return scount == tcount
``````

### # 排序

``````class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
slist = list(s)
tlist = list(t)
return sorted(slist) == sorted(tlist)
``````

## # 日期

2016/4/30 13:08:21 2018 年 11 月 13 日 —— 时间有点快