Description
Bug Report for https://neetcode.io/problems/anagram-groups
I was trying to solve this problem and decided to make a separate method in the class to check if any two given strings from the array is an anagram, but for some reason when I compare the length of two strings in that method it results in a difference of value even if the length of both strings is the same. The second string seems to be incremented by 1. The same method when used in the easy anagram problem, it works fine. Here is the code snippet.
class Solution {
/**
* @param {string[]} strs
* @return {string[][]}
*/
isAnagram(s,t) {
console.log(s.length, t.length)
if(s.length !== t.length) return false;
const newMap1 = new Map();
const newMap2 = new Map();
for(let i = 0; i < s.length; i++) {
if(!newMap1.has(s[i])) {
newMap1.set(s[i], 1);
} else {
newMap1.set(s[i], newMap1.get(s[i]) + 1);
}
if(!newMap2.has(t[i])) {
newMap2.set(t[i], 1);
} else {
newMap2.set(t[i], newMap2.get(t[i]) + 1);
}
}
for(const [char1, count1] of newMap1) {
if(!newMap2.has(char1)) return false;
if(newMap2.get(char1) !== count1) return false;
}
return true;
}
groupAnagrams(strs) {
const check = this.isAnagram(strs[0], strs[1]);
if(check) return [[strs[0], strs[1]]]
else return [[]]
}
}
I have not completed the groupAnagrams
method so ignore the fact that it won't work for all cases. I was trying to check whether or not the isAnagram
method I wrote is correct.
The test case provided the following string array:
strs = ['at', 'ta'];
Here the console.log
I placed in the isAnagram
method is giving me "2 3" instead of "2 2". The screenshot is given below: