> For the complete documentation index, see [llms.txt](https://liuyang89116.gitbook.io/my-leetcode-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_3_array/problem_242_valid_anagram.md).

# Problem 242: Valid Anagram

> <https://leetcode.com/problems/valid-anagram/>

## 思路

* 这道题至少有两种思路：第一种就是先拆分成数组，然后再转化为 String，比较两个 String 是否相同；第二种就是遍历的方法。
* 第一种方法速度很快，但是可能并不是面试官想要看的
* 最优解放在最上面

```java
/*
  最优解
*/
public class Solution {
    public boolean isAnagram(String s, String t) {
        int[] arr = new int[26];

        for (int i = 0; i < s.length(); i++) {
            arr[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < t.length(); i++) {
            arr[t.charAt(i) - 'a']--;
        }

        for (Integer count : arr) {
            if (count != 0) {
                return false;
            }
        }

        return true;
    }
}
```

```java
/*
  First version
*/
public class Solution {
    public boolean isAnagram(String s, String t) {
        char[] sArr = s.toCharArray();
        char[] tArr = t.toCharArray();
        Arrays.sort(sArr);
        Arrays.sort(tArr);

        return String.valueOf(sArr).equals(String.valueOf(tArr)); 
    }
}
```

```java
/*
  Second version
*/
public class Solution {
    public boolean isAnagram(String s, String t) {
        HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
        char[] sArr = s.toCharArray();
        char[] tArr = t.toCharArray();

        for (Character c : sArr) {
            if (hm.containsKey(c)) {
                hm.put(c, hm.get(c) + 1);
            } else {
                hm.put(c, 1);
            } 
        }

        for (Character c : tArr) {
            if (hm.containsKey(c)) {
                hm.put(c, hm.get(c) - 1);
            } else {
                return false;
            }
        }

        for (Integer value : hm.values()) {
            if (value != 0) {
                return false;
            } 
        }

        return true;
    }
}
```

## 易错点

1.注意如果 map 里面没有该元素的时候，put 的个数是 1

```java
hm.put(c, 1);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_3_array/problem_242_valid_anagram.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
