This is the 16th day of my participation in the August More Text Challenge. For details, see:August is more challenging

# preface

In question 49, the letters are grouped as follows:

Given an array of strings, ask you to put together an alphabetic exagram. The list of results can be returned in any order.

An alphabetic ectopic word is a new word created by rearranging the letters of the original word, all of which are used exactly once.

Example 1:

``Input: STRS = [" eat ", "tea", "tan", "ate" and "NAT" and "bat"] output: [[" bat "], [" NAT ", "tan"], [" ate ", "eat", "tea"]]Copy the code``

Example 2:

``Input: STRS = [""] Output: [[""]]Copy the code``

Example 3:

``Input: STRS = ["a"] Output: [["a"]Copy the code``

# A, thinking

Question 49 – There is an important piece of information in the group of ectopes:

• 六四事件`Letter ectopic words`Is the same characters arranged in different strings **, such as`abc``bac`is`Letter ectopic words`

For ABC and BAC, how do you find the correct two letters in a single traverse?

1. willCharacter sorting in a string,`Letter ectopic words``key`The same
2. `Hash table`Is stored in the`key`So that`Letter ectopic words`Can be grouped correctly

To sum up, our overall idea is: sort + hash table

### For example

Here in STRS = [” eat “, “tea”, “tan”, “ate” and “NAT”, “bat”) as an example

1. `i=0`When,`strs[0] = "eat"`sorted`key = "aet"`. At this time`map`Do not contain it`key`, so it will be`"eat"`Put in new`List`
2. `i=1`When,`strs[1] = "tea"`sorted`key = "aet"`. At this time`map`This is contained in`key`That will be`"tea"`Put in the`key`The corresponding`List`
3. `i=2`When,`strs[2] = "tan"`sorted`key = "ant"`. At this time`map`Do not contain it`key`, so it will be`"tan"`Put in new`List`
4. `i=3`When,`strs[3] = "ate"`sorted`key = "aet"`. At this time`map`This is contained in`key`That will be`"ate"`Put in the`key`The corresponding`List`
5. `i=4`When,`strs[4] = "nat"`sorted`key = "ant"`. At this time`map`This is contained in`key`That will be`"nat"`Put in the`key`The corresponding`List`
6. `i=5`When,`strs[5] = "bat"`sorted`key = "abt"`. At this time`map`Do not contain it`key`, so it will be`"bat"`Put in new`List`
7. Eventually you get the right one`Letter ectopic words`grouping

# Second, the implementation

### The implementation code

The implementation code is consistent with the idea

``````    public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String str : strs) {
char[] array = str.toCharArray();
Arrays.sort(array); / / sorting
String key = new String(array);
List<String> list;
if (map.containsKey(key)) { // Check whether the key exists
list = map.get(key);
} else {
list = new ArrayList<>();
}
map.put(key, list);
}
return new ArrayList<>(map.values());
}
Copy the code``````

### The test code

``````    public static void main(String[] args) {
String[] strs = {"eat"."tea"."tan"."ate"."nat"."bat"};
new Number49().groupAnagrams(strs);
}
Copy the code``````

# Third, summary

Thank you for seeing the end, it is my great honor to help you ~♥