Settings Results in 4 milliseconds

How to Find Good Drawing Paper
Category: Art

Advanced Section <p clas ...


Views: 0 Likes: 26
C-Sharp 11 Tips and Tricks
Category: Research

Linq- Did you know you could compair two sequences by using sequence operations li ...


Views: 90 Likes: 58
Visual Studio 2019 Development
Category: Tools

Visual Studio Development Tips ...


Views: 373 Likes: 129
Tips Tricks When Drawing Realistic Photo
Category: Art

One of the important mistakes to avoid when drawing a detailed picture is to damage th ...


Views: 0 Likes: 29
Here is what is going on this week at ErnesTech.co ...
Category: General

Hello readers,Inside this article, you will read about "Why ErnesTech is Developing o ...


Views: 0 Likes: 48
How to Use a well Known Drawing Method to Achieve ...
Category: Art

The Grid Method is a method of drawing an outline from a reference photo onto paper. T ...


Views: 0 Likes: 35
ASP.NET 8 Best Practices: Coding, Performance Tips ...
Category: .Net 7

In this chapter, we will explore various best practices and performance tips to enhance your ASP. ...


Views: 368 Likes: 98
What is the best practices for creating a robots.t ...
Category: Research

Creating a robots.txt file is an important step in protecting your website from malicious bots a ...


Views: 0 Likes: 24
Everything Access Tutorials
Category: Technology

<span style="font-size medium; font-weight bold; textline under ...


Views: 327 Likes: 82
How to Prompt ChatGPT and Google Bard for Best Res ...
Category: Machine Learning

Here are some tips for prompting Google Bard Be specific. The more specific you a ...


Views: 0 Likes: 34
How to draw an eye in eight steps
Category: Art

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In this blog post, I will ex ...


Views: 447 Likes: 104
Front-End Bootstrap Development Notes Tips and Tri ...
Category: .Net 7

How to remove borders from a Boostrap 5 Table Do you find yourself adding "border-0" to e ...


Views: 0 Likes: 27
Steps to Creating the Drawing Outlines
Category: Art

Beginner&rsquo;s Section ...


Views: 0 Likes: 13
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!! Ruby on Rails Server Wont Start Error
Category: Technology

When you run "rails s -b 0.0.0.0" into a bash command console and the Rails Server does not start, l ...


Views: 365 Likes: 92
SQL Server Tips and Tricks
Category: SQL

Error Debugging Did you know you could double click on the SQL Error and ...


Views: 0 Likes: 44
Kint Debugging not Showing (Drupal 8 Web Developme ...
Category: Technology

Tips and Tricks ...


Views: 263 Likes: 87
Nginx: Kill all Nginx process on Linux and Run a c ...
Category: Linux

Nginx Tips and Tricks Tip 1. Kill all process for nginx [If there are pr ...


Views: 893 Likes: 110
Entity Framework Core 6 and 7 Tips and Tricks
Category: Entity Framework

Interceptors in EF Core 7- Use the TPC (Table per Concrete) Mapping strategy (prefer ...


Views: 0 Likes: 29
Clean JSON Data in Excel
Category: Databases

When working with data, it is important to know tips and tricks that will save you a lot of time. As ...


Views: 320 Likes: 105
Front-End Development Tips and Tricks
Category: Front-End

1. Did you know you could make a form (Html Tag) a parent of different other <st ...


Views: 45 Likes: 61

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]