# Group Anagrams

Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.

An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

> Example 1 :\
> Input : strs = \["eat","tea","tan","ate","nat","bat"]\
> Output : \[\["bat"],\["nat","tan"],\["ate","eat","tea"]]
>
> Example 2 :\
> Input : strs = \[""]\
> Output : \[\[""]]
>
> Example 3 :\
> Input : strs = \["a"]\
> Output : \[\["a"]]

**알고리즘**\
1\. 자료구조 만든다. - Ma과 정답List\<List\<String>>\
2\. Input String을 char\[ ] 배열로 바꾼다. - String.toCharArray() 이용\
&#x20;  이를 알파벳순으로 정렬한다. - Arrays.sort()이용.(알파벳순으로 정렬)\
3\. 2를 다시 String으로 바꾸고, 이것은 map의 key가 된다! - String.valueOf()이용\
4\. Map에 넣는다! Map(key,value) = Map(String, List\<String>). key는 일종의 index, value는 일종의 데이터라고 생각하면 편함!\
5\. 정답이 되는 List\<List\<String>>를 리턴한다.map의 모든 value를 담는다. - .addAll(map.values()) 이용

**알고리즘을 Java로**

```java
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        //0.기저사례 제외
        if (strs == null || strs.length == 0) return new ArrayList<List<String>>();
        //1.그릇 생성
        List<List<String>> result = new ArrayList<>();
        Map<String,List<String>> map = new HashMap<String,List<String>>();
        
        for(String str : strs) {
            //2-1.String을 char[]로 바꾼다.
            //2-2.Arrays.sort()를 이용하여 2-1을 정렬한다!
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);

            //3.정렬한 char 배열의 값은 String이 되고, 이것은 map의 key가 된다.
            //3-1. char[]를 다시 String으로 바꾼다.
            //3-2. 3-1을 map의 key로 만든다(저장한다).
            String key = String.valueOf(charArray);
        
            //4.Map(key,value) = Map(String,List<String>)
            if(map.containsKey(key)) {//key값으로 aet가 존재한다면!
                map.get(key).add(str);
            }else {//처음엔 else문 실행!
                List<String> list = new ArrayList<>();
                list.add(str);
                map.put(key,list);
            }
        }
        result.addAll(map.values());
        return result;
    }
}
```

**배운 내용 정리**\
1\. String.toCharArray() : String을 char\[ ] 배열로 바꾼다.(**빈출,중요!**)\
2\. String.valueOf(parameter) : 인자로 전달받은 것을 String으로 바꾼.\
3\. Map.values() : Map의 모든 values를 가져온다.\
4\. ArrayList.addAll() : ArrayList의 add() 메서드와 addAll() 메서드는 ArrayList에 인자로 전달받은 것을 데이터로 추가한다. addAll()은 ArrayList를 통째로 추가할 때 사용된다.

```java
result.addAll(map.values());
//map.values()를 통해 map의 모든 value를 가져오고,
//이 때 map의 value는 String 타입의 List이고, 이 List는 ArrayList를 통해 만들어짐!

```

<br>


---

# Agent Instructions: 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://heunnajo.gitbook.io/algorithms-problem-solving-skills/algorithm-problems/group-anagrams.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.
