Skip to content

Bug Report for is-anagram #4727

@graham-macmaster

Description

@graham-macmaster

Bug Report for https://neetcode.io/problems/is-anagram

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.


I'm running into a weird issue with this problem. The following code works fine for the LeetCode version here: https://leetcode.com/problems/valid-anagram/, but runs into an issue with a "reflect" import that I can't see. I imagine a "reflect" import is being injected unnecessarily for the Go language on this problem?

Edit: I guess it's there because you expect people to use reflect.DeepEqual to do some kind of comparison between slices, but still I think the below code should run, even if it's sub-optimal.

Language: Go

My code:

func isAnagram(s string, t string) bool {
    
    if len(s) != len(t) {
        return false
    }

    occurrences := make(map[rune]int)

    for _, c := range s {
        occurrences[c]++
    }

    for _, c := range t {
        if _, ok := occurrences[c]; !ok {
            return false
        }

        occurrences[c]--
    }

    for _, count := range occurrences {
        if count > 0 {
            return false
        }
    }

    return true
}

Error message:

# command-line-arguments
./main.go:-4:2: "reflect" imported and not used
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions