标签:字符串
242. Valid Anagram
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.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size()) { return false; } else { int sBit[26] = {0};//记录每个字母出现的次数 int tBit[26] = {0}; const char * sp = s.c_str(); const char * tp = t.c_str(); for(int i = 0; i < s.size() ; i++) { sBit[*(sp+i) - ‘a‘]++; tBit[*(tp+i) - ‘a‘]++; } for(int j = 0; j < 26;j++) { if(sBit[j] != tBit[j]) { return false; } } return true; } } };
2016-08-05 13:52:07
本文出自 “做最好的自己” 博客,请务必保留此出处http://qiaopeng688.blog.51cto.com/3572484/1834716
leetCode242. Valid Anagram 合法的由颠倒字母顺序而构成的字 sort
标签:字符串
原文地址:http://qiaopeng688.blog.51cto.com/3572484/1834716