242. Valid Anagram

    xiaoxiao2021-03-25  76

    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?

    我采用unordered_map,时间复杂度O(n):

    class Solution { public: bool isAnagram(string s, string t) { if(s.length() != t.length()) return false; if(s.length() == 0) return true; unordered_map<char, int> umap; for(auto i : s) umap[i]++; for(auto i : t) umap[i]--; auto it = std::find_if(umap.begin(), umap.end(), [](const pair<char, int>& pr) { return pr.second != 0; }); return it == umap.end(); } };
    转载请注明原文地址: https://ju.6miu.com/read-37905.html

    最新回复(0)