leetcode string 242 valid letter dyslexia

THE

problem

Insert picture description here

solution

Code



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

-  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;


   }
};


Summary and reflection

  1. Pay attention to exchange issues.

Guess you like

Origin blog.csdn.net/liupeng19970119/article/details/114185031