12 ms c++ solution, only basic arrays are used


  • 0
    L
        class Solution {
    public:
        bool isAnagram(string s, string t) {
            int first[26] = {};
            int second[26] = {};
            int i,temp1,temp2;
            
            if(s.size() != t.size()){
                return false;
            }else{
                for(i=0;i<s.size();i++){
                    temp1 = (int)s[i] - 97;
                    temp2 = (int)t[i] - 97;
                    first[temp1] += 1;
                    second[temp2] += 1;
                }
                
                for(i=0;i<26;i++){
                    if(first[i] != second[i]){
                        return false;
                    }
                }
                return true;
            }
        }
    };

  • 0
    L

    why dont u use dict? it is more convenient


  • 0
    L

    Thank you le0192 for your question. I am a totally programming newbie and I learned a lot from your advice.

    Basically I would still use my original solution because people don't have to use any library to finish this job, and the basic idea using dict(or STL maps in C++) is similar to my idea. I am not sure the complexity using the library is less than using basic functions, if you know that, please comment below and let me know. Thanks again for your advice!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.