---
description: Hash table
---

# 242. Valid Anagram

## 1.問題

* 給予字串s, t, 決定t是否是s的重新排序字

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LJDEYcFwJuU7Pa6wKko%2F-LJDOUZH_JFQEB3hANQ9%2F2018080303.jpg?alt=media\&token=b20104c4-0d70-457a-8f98-230d49cc5be4)

## 2.想法

* 紀錄每個字元出現的次數

## 3.程式碼

```
class Solution {
public:
    bool isAnagram(string s, string t) {
        int size1 = s.size();
        int size2 = t.size();
        if (size1 == 0 && size2 ==0)
        {
            return true;
        }
        vector<int> cnt1(128, 0);
        vector<int> cnt2(128, 0);
        for (int i = 0; i < size1; i++)
        {
            cnt1[s[i]]++;
        }
        for (int i = 0; i < size2; i++)
        {
            cnt2[t[i]]++;
        }
        for (int i = 0; i < 128; i++)
        {
            if (cnt1[i] != cnt2[i])
            {
                return false;
            }
        }
        return true;
    }
};
```

## 4.Performance

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LJDEYcFwJuU7Pa6wKko%2F-LJDOkWNLlzi5mQCU565%2F2018080304.jpg?alt=media\&token=13512b4e-f3db-4898-96d3-daa5395ffb9a)
