写出一个函数 anagram(s, t)
判断两个字符串是否可以通过改变字母的顺序变成一样的字符串。
What is Anagram?
- Two strings are anagram if they can be the same after change the order of characters.
给出 s = "abcd"
,t="dcab"
,返回 true
.
给出 s = "ab"
, t = "ab"
, 返回 true
.
给出 s = "ab"
, t = "ac"
, 返回 false
.
class Solution {
public:
/**
* @param s: The first string
* @param b: The second string
* @return true or false
*/
bool anagram(string s, string t) {
// write your code here
int *a=new int[256] ();
for(int i=0;i<s.size();i++){
a[s[i]]++;
a[t[i]]--;
}
for(int i=0;i<256;i++){
if(a[i]>0||a[i]<0){
return false;
}
}
delete [] a;
return true;
}
};