C
C#17mo ago
morry329#

✅ 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 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
LeetCode - The World's Leading Online Programming Learning Platform
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.
3 Replies
canton7
canton717mo ago
Have you tried to debug it? Step through it in a debugger, see why it is or isn't adding items to result
Pobiega
Pobiega16mo ago
You have posted this before and gotten comments on it before. Have you acted on those comments? Specifically the fact that the list is empty because you are not checking for anagrams as intended
morry329#
morry329#OP16mo ago
Yes, I acted on this and my code result is better than before (I mean before I posted my first question on this). My status quo is the code compiles but it did not return anything (as though I have forgotten the function to print out the list) - the list should add something. ok i think I found the partial fix. The error was the bool method IsAnagram, it did not check correctly if my window is an anagram or not
Want results from more Discord servers?
Add your server