首页 > 其他分享 >242. 有效的字母异位词

242. 有效的字母异位词

时间:2023-02-26 15:55:36浏览次数:35  
标签:return string nums ++ 异位 字母 iter num 242

 1 class Solution {
 2 public:
 3     bool isAnagram(string s, string t) {
 4         if (s.size() != t.size()) return false;
 5         string::iterator s_iter = s.begin();
 6         string::iterator t_iter = t.begin();
 7         map<char, int> s_nums;
 8         map<char, int> t_nums;
 9         while (s_iter != s.end())
10         {
11             ++s_nums[*s_iter++];
12             ++t_nums[*t_iter++];
13         }
14         //判断map是否一样
15         for (auto p = s_nums.begin(); p != s_nums.end(); p++)
16         {
17             int s_num = s_nums[p->first];
18             if (!t_nums[p->first]) return false;
19             int t_num = t_nums[p->first];
20             if (s_num != t_num)
21             {
22                 return false;
23             }
24         }
25         return true;  
26     }
27 };

 

标签:return,string,nums,++,异位,字母,iter,num,242
From: https://www.cnblogs.com/lihaoxiang/p/17156830.html

相关文章