Skip to content

Comments

Add implementation for Hashing-1#2263

Open
rishigoswamy wants to merge 1 commit intosuper30admin:masterfrom
rishigoswamy:master
Open

Add implementation for Hashing-1#2263
rishigoswamy wants to merge 1 commit intosuper30admin:masterfrom
rishigoswamy:master

Conversation

@rishigoswamy
Copy link

No description provided.

@super30admin
Copy link
Owner

Strengths:

  • The solution correctly groups anagrams using a frequency array, which is an efficient method.
  • The code is clean and easy to understand, with good comments explaining the approach.
  • The use of defaultdict simplifies the code by automatically initializing lists.

Areas for Improvement:

  • The import statement "import List" is incorrect. In Python, for type hints, you should use "from typing import List". This will cause a runtime error if not corrected.
  • The solution includes two other files (leetcode205.py and leetcode290.py) that are not relevant to the problem. When submitting a solution, it should only contain the code for the problem at hand.
  • The variable name "hMap" could be more descriptive, such as "anagram_groups".
  • The loop "for key in hMap" is fine, but you can directly use "list(hMap.values())" to return the groups without building a list manually.

Suggested corrected code for leetcode49.py:

from typing import List
from collections import defaultdict

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        anagram_groups = defaultdict(list)
        for word in strs:
            char_count = [0] * 26
            for char in word:
                char_count[ord(char) - ord('a')] += 1
            anagram_groups[tuple(char_count)].append(word)
        return list(anagram_groups.values())

@super30admin
Copy link
Owner

Your solution for grouping anagrams is well-implemented. The approach of using a frequency array as a key is efficient and avoids the overhead of sorting each string. The code is readable and follows good practices.

Strengths:

  • Correct use of a frequency array to generate unique keys for anagram groups.
  • Efficient use of defaultdict to collect groups.
  • Clear and concise code.

Areas for improvement:

  • The import statement "import List" is not standard. For type hints, you should use "from typing import List" if you are using Python versions below 3.9. Alternatively, you can use the built-in list type in Python 3.9+ without importing.
  • You included solutions for other problems (isomorphic strings and word pattern) in the same submission. For a focused evaluation, it's better to submit only the relevant code. Ensure that your solution file contains only the code for the problem at hand.

Overall, your solution is correct and efficient. Just pay attention to the imports and keep your submissions focused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants