✅ The code prints out no list elements
So I have been experimenting with this sample solution for this puzzle https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/?currentPage=1&orderBy=hot&query=&tag=c-2
This code returns no list elements whilst it returns no exception/errors. Maybe can anyone explain to me what went wrong with it?
public class Solution2
{
public List<int> FindAnagrams(string s, string p)
{
if (p.Length > s.Length)
{
return new List<int>();
}
List<int> result = new List<int>();
int[] mapP = new int[26];
foreach (var symbol in p)
{
mapP[symbol - 'a']++;
}
for (int i = 0; i < p.Length; i++)
{
if (IsAnagram(mapP))
{
result.Add(i - p.Length);
}
}
for (int i = p.Length; i < s.Length; i++)
{
mapP[s[i-p.Length] - 'a']++;
mapP[s[i] - 'a']--;
if (IsAnagram(mapP))
{
result.Add(i-p.Length+1);
}
}
Console.WriteLine($"return the list 2 ");
result.ForEach(Console.WriteLine);
return result;
}
bool IsAnagram(int[] map)
{
foreach (var count in map)
{
if (count != 0)
{
return false;
}
}
return true;
}
}This code returns no list elements whilst it returns no exception/errors. Maybe can anyone explain to me what went wrong with it?
LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
