Back to course home
0% completed
Vote For New Content
My solution, one of the test cases is incorrect
Amber Wolf
Apr 16, 2025
This was my solution to the question. There is however one test case in the web browser IDE that is incorrect. In my solution I accounted for a string that can be given that is empty, which by definition is not an anagram. So a string "" being compared to any other string, including "" should be false as that is not an anagram.
public bool isAnagram(string s, string t) { // using Regex to massage the strings and remove any special characters/white spaces var trimmedS = Regex.Replace(s, "[^a-zA-Z0-9]", "").ToLower(); var trimmedT = Regex.Replace(t, "[^a-zA-Z0-9]", "").ToLower(); // first checking the length of the words, if they are a different amount of charcters or they contain no characters they cannot be an anagram. if (trimmedS.Length != trimmedT.Length || (trimmedS.Length <= 0|| trimmedT.Length <= 0)) { return false; } // order the string alphabetically trimmedS = new string(trimmedS.OrderBy(c => c).ToArray()); trimmedT = new string(trimmedT.OrderBy(c => c).ToArray()); //if the strings match they are an anagram otherwise they are not. if (trimmedS == trimmedT) { return true; } else { return false; } }
0
0
Comments
Comments