My C++ Solution


  • 13
    T
    class Solution {
    public:
        bool isAnagram(string s, string t) {
            if(s.size() != t.size()) return false;
            int a[26] = {0};
            for(int i = 0; i < s.size(); i ++) {
                a[s[i]%26]++;
                a[t[i]%26]--;
            }
            for(int i = 0; i < 26; i++) {
                if(a[i] != 0) return false;
            }
            return true;
        }
    };

  • 0
    L

    good solution


  • 0
    L

    int a[26] = {};
    is enough.
    int a[26] = {0};
    may mislead the readers that they can use
    int a[26] = {1};
    to initialize all the elements to 1.


  • 0
    T

    you are right, thank you!


Log in to reply
 

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