【字符串】面试题 01.02. 判定是否互为字符重排

题目:

解答:

思路:哈希。

 1 class Solution {
 2 public:
 3     bool CheckPermutation(string s1, string s2) 
 4     {
 5         if (s1.size() != s2.size()) 
 6         {
 7             return false;
 8         }
 9         vector<int> hash(256, 0);
10         for (int i = 0; i < s1.size(); ++i) 
11         {
12             ++hash[s1[i]];
13             --hash[s2[i]];
14         }
15         for (int i = 0; i < hash.size(); ++i) 
16         {
17             if (hash[i] != 0) 
18             {
19                 return false;
20             }
21         }
22         return true;
23 
24     }
25 };

猜你喜欢

转载自www.cnblogs.com/ocpc/p/12824640.html
今日推荐