For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return 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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | bool isAnagram(char* s, char* t) { int sizeS=strlen(s); int sizeT=strlen(t); if(sizeS!=sizeT)return false; int ASCIIS[256]; int ASCIIT[256]; memset(ASCIIS,0,256*sizeof(int)); memset(ASCIIT,0,256*sizeof(int)); for(int i=0;i<sizeS;i++){ ASCIIS[(int)s[i]]+=1; ASCIIT[(int)t[i]]+=1; } int fail=0; for(int i=0;i<256;i++){ if( ASCIIS[i]!= ASCIIT[i]){ fail=1; return false; } } return fail==0? true:false; } |
沒有留言:
張貼留言