leetcode string 242 有效的字母异位词

O

问题

在这里插入图片描述

解决方案

代码



/*
思路: 字符串匹配问题, 利用哈希表和遍历解决。 

-  define numsone numstwo maps to  save both data
-  for  iterator i  in nums:
- -  if numsone[i] = numstwo[i] contine 
- -  else break;
- return false;
*/

class Solution {
    
    
public:
   bool isAnagram(string s, string t) {
    
    
       unordered_map<char,int> numone, numtwo;
       for(char a: s){
    
    
           numone[a]++;
       }
       for(char a: t){
    
    
           numtwo[a]++;
       }
       if(numone.size()<numtwo.size()){
    
    
           unordered_map<char,int> temp = numone;
           numone = numtwo;
           numtwo = temp;
       }
       for(unordered_map<char,int>::iterator i = numone.begin();i!=numtwo.end();i++){
    
    
           if(numone[(*i).first] == numtwo[(*i).first]){
    
    
               continue;
           }
           else return false;

       }

       return true;


   }
};


总结与反思

  1. 注意交换问题。

猜你喜欢

转载自blog.csdn.net/liupeng19970119/article/details/114185031