1
2
3
4
|
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. |
题意:给的两个字符串判断是否是相同字符组成的不同字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class Solution {
public boolean isAnagram(String s, String t) {
int [] ret= new int [ 26 ];
for ( int i= 0 ;i<s.length();i++){
ret[s.charAt(i)- 'a' ]--;
}
for ( int i= 0 ;i<t.length();i++){
++ret[t.charAt(i)- 'a' ];
}
for ( int i= 0 ;i< 26 ;i++){
if (ret[i]!= 0 )
return false ;
}
return true ;
}
} |
PS:参见之前的那个勒索信的那个,直接hash求出每个字符的数量即可
本文转自 努力的C 51CTO博客,原文链接:http://blog.51cto.com/fulin0532/1890860