Settings Results in 4 milliseconds

DASH Streaming Video from the Web
Category: Technology

Video Streaming Notes Online video Option  Video has ...


Views: 388 Likes: 91
Software Development Refactoring Wisdom I gained t ...
Category: Software Development

Software Development Refactoring Wisdom I gained through R ...


Views: 175 Likes: 84
What is a Host in Asp.Net 8 Application
Category: Research

IntroductionIn an ASP.NET Core application, a host is responsible for starting an ...


Views: 0 Likes: 31
Asp.Net MVC Development Notes
Category: .Net 7

<a href="https//www.freecodecamp.org/news/an-awesome-guide-on-how-to-build-restful-apis-w ...


Views: 752 Likes: 79
The 'Microsoft.AspNetCore.Mvc.ViewFeature.Infrastr ...
Category: .Net 7

Question How do you solve this error in Asp.Net 6? "<span style="background-col ...


Views: 0 Likes: 59
Determine if two strings are anagrams with C# .NET
Determine if two strings are anagrams with C# .NET

Two strings are anagrams if they are made up of the same set of characters. Examples “hello” and “loleh”“123123” and “312312”“qwerty” and “wretqy” The degree of “anagram-ness” can vary ignore case?ignore non-numeric characters?ignore whitespace? In this post we’ll only consider word-characters only and the comparison will be case-insensitive to make the problem more interesting. We’ll write a function that accepts two integers and returns a boolean true if the strings are anagrams, otherwise false. We’ll look at two solutions out of many that exist out there using a character mapusing string sort What is a character map? It is a map where the key is of type char and the value if of type integer. We collect the unique characters of a string and count how many times each character occurs in the string. E.g. CharCount‘f’2‘g’1‘i’2‘d’1‘o’1 We do that for both strings and compare the counts of each unique character. Let’s start with a skeleton using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Algorithms { public class Anagram { public bool AreAnagrams(string first, string second) { if (first == null || second == null) return false; return AreAnagramsWithCharMap(first, second); //return AreAnagramsWithSortedStrings(first, second); } private bool AreAnagramsWithCharMap(string first, string second) { return false; } private bool AreAnagramsWithSortedStrings(string first, string second) { return false; } private Dictionary<char, int> MakeCharacterMap(string input) { string cleaned = CleanInput(input); return cleaned.Distinct().ToDictionary(c => c, c => cleaned.Count(s => s == c)); } private string CleanInput(string input) { return Regex.Replace(input, @"[_]+|[^\w]+|[\d-]+", "").ToLower(); } } } We start by some null-checking and return false if either of the two inputs is null. The AreAnagramsWithCharMap function has been wired up but it’s not yet implemented. The function for the second solution AreAnagramsWithSortedStrings has also been prepared. We have two private functions as well CleanInputthis one takes a string and strips all underscores, white space and non-word characters from it and returns the lower-case version of itMakeCharacterMapfirst we clean the incoming input stringsecond we use a couple of LINQ operators to build the character mapDistinct() to gather the unique characters from the stringToDictionary() to build the map itself, the key will be the character itself and for the value we count the number of occurrences of that character in the string Let’s look at the implementation of AreAnagramsWithCharMap private bool AreAnagramsWithCharMap(string first, string second) { var charMapFirst = MakeCharacterMap(first); var charMapSecond = MakeCharacterMap(second); if (charMapFirst.Count != charMapSecond.Count) { return false; } return charMapFirst.All(kvp => charMapSecond.ContainsKey(kvp.Key) ? kvp.Value == charMapSecond[kvp.Key] false); } We first create the two character maps. If they differ in size then we can immediately return false. It means that one of the strings has at least one more character than the other so it’s pointless to continue. Otherwise we make use of the All LINQ operator which return true of all the elements of a collection fulfil a certain condition. The condition is based on two parameters whether the character map contains the character as the key in the first placewhether that character occurs with the same frequency as in the source map If both conditions are fulfilled for all characters in the character maps then we return true. Here’s a set of unit tests using System; using System.Collections.Generic; using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Algorithms.Tests { [TestClass] public class AnagramTests { [TestMethod] public void AreAnagramsTests() { Anagram a = new Anagram(); Assert.IsTrue(a.AreAnagrams("hello", "___ hllOe!! 456 ???")); Assert.IsFalse(a.AreAnagrams("sdfs", null)); Assert.IsFalse(a.AreAnagrams(null, "sdfs")); Assert.IsFalse(a.AreAnagrams(null, null)); Assert.IsFalse(a.AreAnagrams("qwerty", "yewr")); Assert.IsFalse(a.AreAnagrams("qwerty", "qwertyuiop")); Assert.IsTrue(a.AreAnagrams("? par**lIame%%nt !", "partIAL men")); Assert.IsTrue(a.AreAnagrams("a gentleman", "elegant man")); } } } The will all pass. The second solution takes the two cleaned strings, puts them in order and compares them. This solution is slightly less performant than the first one due to the string ordering though. The map comparison doesn’t involve any ordering so it’s somewhat quicker. Here’s the implemented function private bool AreAnagramsWithSortedStrings(string first, string second) { string sortedOne = string.Concat(CleanInput(first).OrderBy(c => c)); string sortedTwo = string.Concat(CleanInput(second).OrderBy(c => c)); return sortedOne == sortedTwo; } We again clean the input string and then call the OrderBy LINQ operator. It returns an ordered collection of characters from the string, i.e. not an ordered string. Hence we need to embed this bit of code in string.Concat so that we build the string again from the characters. Finally we simply compare the two strings. Wire up this function from the main one and rerun the unit tests. They will still pass. public bool AreAnagrams(string first, string second) { if (first == null || second == null) return false; //return AreAnagramsWithCharMap(first, second); return AreAnagramsWithSortedStrings(first, second); }


Solved: How NOT To Show XML In Json Returned By AS ...
Category: Technology

ProblemRetur ...


Views: 259 Likes: 105
OverflowException: Value was either too large or t ...
Category: .Net 7

Question How do you solve for the error that says "OverflowException Value was ...


Views: 0 Likes: 34
How do you get an ID of recording getting inserted ...
Category: SQL

To get the identity value (the insertion_id) that corresponds to each record getting inserted int ...


Views: 0 Likes: 50
Asp.Net 5 Development Notes (DotNet Core 3.1 Study ...
Category: Software Development

Study Notes to use when progra ...


Views: 423 Likes: 61
RuntimeBinderException: The best overloaded method ...
Category: .Net 7

Question How do you resolve the C-Sharp <a class='text-decoration-none' href='https//www.ernes ...


Views: 0 Likes: 33
[Entity Framework Core 5] An Unhandled Exception O ...
Category: Entity Framework

Question How do you solve the Entity Framework Core Error "An Unhandled Exception Occurred Whil ...


Views: 302 Likes: 90
How to get localized string from ASP.NET Core Cont ...
Category: .Net 7

Question <a class='text-decoration-none' href='https//www.ernestech.com/articles/search?query= ...


Views: 0 Likes: 38
How to Setup the Development Environment using Vis ...
Category: Computer Programming

<img style="float right; border-style ...


Views: 0 Likes: 23
What is New in C-Sharp 9 Programming Language
Category: .Net 7

C# is one of the high-level programming languages, it is used in many business applications, Game ...


Views: 278 Likes: 102
How to Use Spans in C-Sharp ( Stackalloc, Heap and ...
Category: .Net 7

Read about Spans ...


Views: 145 Likes: 67
How to check if the number is empty in C-Sharp Asp ...
Category: HTML5

Question Is there a way to check if the number is empty in C#? There are so ma ...


Views: 0 Likes: 27
What does TRIM function do in T-SQL
Category: Research

Title Understanding the TRIM Function in T-SQL A Comprehensive Guide with Code Examples and Vi ...


Views: 0 Likes: 39
How do you use CONCAT_WS function in T-SQL
Category: Research

Title Mastering String Concatenation in T-SQL with CONCAT_WS FunctionIn the realm ...


Views: 0 Likes: 35

Login to Continue, We will bring you back to this content 0



For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email Address[email protected]