Making Anagrams challenge

This commit is contained in:
Guillermo Marcel 2025-02-14 14:28:42 -03:00
parent 9874f28319
commit eb4b31c566
4 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MakingAnagrams", "MakingAnagrams\MakingAnagrams.csproj", "{21E7CA7A-4813-4499-AEAA-93E33E6A936C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{21E7CA7A-4813-4499-AEAA-93E33E6A936C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21E7CA7A-4813-4499-AEAA-93E33E6A936C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21E7CA7A-4813-4499-AEAA-93E33E6A936C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21E7CA7A-4813-4499-AEAA-93E33E6A936C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,56 @@

using Microsoft.VisualBasic.CompilerServices;
_ = Result.makeAnagram("aaa", "aaa");
_ = Result.makeAnagram("aba", "baa");
Result.makeAnagram("aba", "abb");
Result.makeAnagram("mmm", "mm");
Result.makeAnagram("mm", "mmm");
_ = Result.makeAnagram("fcrxzwscanmligyxyvym", "jxwtrhvujlmrpdoqbisbwhmgpmeoke");
Console.ReadKey();
class Result
{
/*
* Complete the 'makeAnagram' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. STRING a
* 2. STRING b
*/
public static int makeAnagram(string a, string b)
{
char[] auxA = a.ToArray();
char[] auxB = b.ToArray();
//double for per each array
var diff = auxA.Length + auxB.Length;
for (int i = 0; i < auxA.Length; i++)
{
for (int j = 0; j < auxB.Length; j++)
{
var ca = auxA[i];
var cb = auxB[j];
if (auxA[i] == auxB[j])
{
auxA[i] = '_';
auxB[j] = '_';
diff -= 2;
break;
}
}
}
// var diff = auxA.Count(x => x != '_') + auxB.Count(x => x != '_');
//Console.WriteLine($"{new string(auxA)} <-> {new string(auxB)} ({diff})");
return diff;
}
}

View File

@ -0,0 +1,46 @@
https://www.hackerrank.com/challenges/ctci-making-anagrams/problem?h_l=interview&isFullScreen=false&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings
A student is taking a cryptography class and has found anagrams to be very useful. Two strings are anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency. For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.
The student decides on an encryption scheme that involves two large strings. The encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Determine this number.
Given two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings.
Example
Delete from and from so that the remaining strings are and which are anagrams. This takes character deletions.
Function Description
Complete the makeAnagram function in the editor below.
makeAnagram has the following parameter(s):
string a: a string
string b: another string
Returns
int: the minimum total characters that must be deleted
Input Format
The first line contains a single string, .
The second line contains a single string, .
Constraints
The strings and consist of lowercase English alphabetic letters, ascii[a-z].
Sample Input
cde
abc
Sample Output
4
Explanation
Delete the following characters from the strings make them anagrams:
Remove d and e from cde to get c.
Remove a and b from abc to get c.
It takes deletions to make both strings anagrams.