Site icon Wander In Dev

Group Anagrams (LeetCode #49)

Challenge Statement

Constraints

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"]]

Solution

Below is my solution and some test cases. This solution has a linear time complexity O(m+n) and a linear space complexity O(m), where m is the length of the input list strs and n is the max length of the strings in strs.

Exit mobile version