- Notifications
You must be signed in to change notification settings - Fork2.4k
Description
Bug Report forhttps://neetcode.io/problems/anagram-groups
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
I noticed that my solution didnt even get properly tested. I only got the error "Solution is not defined". I dont really know how I got the buf or how to reproduce it, but I can show the code that threw the error:
testing to just return the solution to one test case:
compelte code:
class result {
/**
*@param {string[]} strs
*@return {string[][]}
*
* idea: build a map for each string
* compare map for with map of each group
* if it is in group, add string to group
* else add a new group with the string to the result
*/
groupAnagrams(strs) {
let solution = [["x"]]
return solution
// O(m)
for (const string of strs) {
const currentMap = new Map()
// O(26)
for (const letter of alphabet) {
currentMap.set(letter, 0)
}
// O(n)
for (const letter of string) {
currentMap.set(letter, currentMap.get(letter) + 1)
}
let anagramFound = false
// O(1 + 2 + 3 + 4 + ... + m) = O(m)
for (const resultMap of result) {
// O(n)
if (this.compareMaps(currentMap, resultMap)) {
resultMap.set("result", resultMap.get("result").push(string))
anagramFound = true
}
}
if (!anagramFound) {
currentMap.set("result", string)
result.push(currentMap)
}
}
let finalResult = []
for (const resultMap of result) {
finalResult.push(resultMap.get("result"))
}
return finalResult}/** * @param {Map(), Map()} map1, map2 * @return {bool} * * assumes first map doesnt have a result entry*/compareMaps(map1, map2) { for (key of map1.keys()) { if (map1.get(key) !== map2.get(key)) { return false } } return true}
}