242. Valid Anagram

Problem

Given two strings s and t , write a function to determine if t is an anagram of s.
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?

Example

Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.size()!=t.size())
            return false;
        int temp[256] = {0};
        for(int i=0;i<s.size();i++){
            temp[s[i]]++;
            temp[t[i]]--;
        }
        for(int i=0;i<s.size();i++){
            if(temp[s[i]]!=0)
                return false;
        }
        return true;
    }
};

Result

242. Valid Anagram.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,957评论 0 10
  • 题目: Given two strings s and t, write a function to determ...
    Cloudox_阅读 308评论 0 0
  • 刚进入大学的我,对大学生活一点规划都没有,也就和舍友一样跟风随大流,上课打游戏,宿舍打游戏,周末宅宿舍。 其实我也...
    心行H阅读 733评论 0 0
  • 夕阳柔和的光线落在窗外,花盆里四季牡丹正热烈地盛开。 老秋了,柿子树摇动着红红火火的果儿,菜园里,白菜打起了包,萝...
    觅渡来了阅读 254评论 0 5

友情链接更多精彩内容