From eb4b31c5664b3e4940c51e56ac8cc1c0ef71b844 Mon Sep 17 00:00:00 2001 From: Guillermo Marcel Date: Fri, 14 Feb 2025 14:28:42 -0300 Subject: [PATCH] Making Anagrams challenge --- .../HackerRankChallenges.sln | 16 ++++++ .../MakingAnagrams/MakingAnagrams.csproj | 10 ++++ .../MakingAnagrams/Program.cs | 56 +++++++++++++++++++ .../MakingAnagrams/README.md | 46 +++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 hackerrank/HackerRankChallenges/HackerRankChallenges.sln create mode 100644 hackerrank/HackerRankChallenges/MakingAnagrams/MakingAnagrams.csproj create mode 100644 hackerrank/HackerRankChallenges/MakingAnagrams/Program.cs create mode 100644 hackerrank/HackerRankChallenges/MakingAnagrams/README.md diff --git a/hackerrank/HackerRankChallenges/HackerRankChallenges.sln b/hackerrank/HackerRankChallenges/HackerRankChallenges.sln new file mode 100644 index 0000000..b8a762b --- /dev/null +++ b/hackerrank/HackerRankChallenges/HackerRankChallenges.sln @@ -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 diff --git a/hackerrank/HackerRankChallenges/MakingAnagrams/MakingAnagrams.csproj b/hackerrank/HackerRankChallenges/MakingAnagrams/MakingAnagrams.csproj new file mode 100644 index 0000000..878c604 --- /dev/null +++ b/hackerrank/HackerRankChallenges/MakingAnagrams/MakingAnagrams.csproj @@ -0,0 +1,10 @@ + + + + net9.0 + enable + enable + Exe + + + diff --git a/hackerrank/HackerRankChallenges/MakingAnagrams/Program.cs b/hackerrank/HackerRankChallenges/MakingAnagrams/Program.cs new file mode 100644 index 0000000..e640fe2 --- /dev/null +++ b/hackerrank/HackerRankChallenges/MakingAnagrams/Program.cs @@ -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; + } + +} + diff --git a/hackerrank/HackerRankChallenges/MakingAnagrams/README.md b/hackerrank/HackerRankChallenges/MakingAnagrams/README.md new file mode 100644 index 0000000..fcc6493 --- /dev/null +++ b/hackerrank/HackerRankChallenges/MakingAnagrams/README.md @@ -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. \ No newline at end of file