diff --git a/docs/hackerrank/interview_preparation_kit/string_manipulation/ctci-making-anagrams.md b/docs/hackerrank/interview_preparation_kit/string_manipulation/ctci-making-anagrams.md new file mode 100644 index 00000000..3fe55da0 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/string_manipulation/ctci-making-anagrams.md @@ -0,0 +1,79 @@ +# [Strings: Making Anagrams](https://www.hackerrank.com/challenges/ctci-making-anagrams) + +How many characters should one delete to make two given strings anagrams +of each other? + +- Difficulty: `#easy` +- Category: `#ProblemSolvingBasic` `#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, `a` and `b`, that may or may not be of the same length, +determine the minimum number of character deletions required +to make `a` and `b` anagrams. +Any characters can be deleted from either of the strings. + +## Example + +```python +a = 'cde' +b = 'dcf' +``` + +Delete `e` from `a` and `f` from `b` so that the remaining strings +are `cd` and `dc` which are anagrams. This takes `2` 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, `a`. +The second line contains a single string, `b`. + +## Constraints + +- $ 1 \leq |a|, |b| \leq 10^4 $ +- The strings `a` and `b` consist of lowercase English alphabetic letters, ascii[a-z]. + +## Sample Input + +```text +cde +abc +``` + +## Sample Output + +```text +4 +``` + +## Explanation + +Delete the following characters from the strings make them anagrams: + +1. Remove `d` and `e` from `cde` to get `c`. +2. Remove `a` and `b` from `abc` to get `c`. + +It takes `4` deletions to make both strings anagrams. diff --git a/src/hackerrank/interview_preparation_kit/string_manipulation/ctci_making_anagrams.test.ts b/src/hackerrank/interview_preparation_kit/string_manipulation/ctci_making_anagrams.test.ts new file mode 100644 index 00000000..48650104 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/string_manipulation/ctci_making_anagrams.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from '@jest/globals'; + +import { makeAnagram } from './ctci_making_anagrams'; +import TEST_CASES from './ctci_making_anagrams.testcases.json'; + +describe('makeAnagram', () => { + it('makeAnagram test cases', () => { + expect.assertions(3); + + TEST_CASES.forEach((test) => { + const result = makeAnagram(test.a, test.b); + + expect(result).toStrictEqual(test.expected); + }); + }); +}); diff --git a/src/hackerrank/interview_preparation_kit/string_manipulation/ctci_making_anagrams.testcases.json b/src/hackerrank/interview_preparation_kit/string_manipulation/ctci_making_anagrams.testcases.json new file mode 100644 index 00000000..e5d44d02 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/string_manipulation/ctci_making_anagrams.testcases.json @@ -0,0 +1,20 @@ +[ + { + "title": "Sample Test case 0", + "a": "cde", + "b": "abc", + "expected": 4 + }, + { + "title": "Sample Test case 1", + "a": "fcrxzwscanmligyxyvym", + "b": "jxwtrhvujlmrpdoqbisbwhmgpmeoke", + "expected": 30 + }, + { + "title": "Sample Test case 2", + "a": "showman", + "b": "woman", + "expected": 2 + } +] diff --git a/src/hackerrank/interview_preparation_kit/string_manipulation/ctci_making_anagrams.ts b/src/hackerrank/interview_preparation_kit/string_manipulation/ctci_making_anagrams.ts new file mode 100644 index 00000000..cd4af840 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/string_manipulation/ctci_making_anagrams.ts @@ -0,0 +1,37 @@ +/** + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/string_manipulation/m]] + */ + +function charToDicMap(word: string): Record { + const output: Record = {}; + + for (const letter of word.split('')) { + output[letter] = output?.[letter] ? output[letter] + 1 : 1; + } + + return output; +} + +function sum(values: number[]): number { + return values.reduce( + (previousValue: number, currentValue: number) => + previousValue + currentValue, + 0 + ); +} + +export function makeAnagram(a: string, b: string): number { + const aMap = charToDicMap(a); + const bMap = charToDicMap(b); + + for (const key of Object.keys(aMap)) { + if (bMap?.[key]) { + aMap[key] = Math.abs(aMap[key] - bMap[key]); + bMap[key] = 0; + } + } + + return sum(Object.values(aMap)) + sum(Object.values(bMap)); +} + +export default { makeAnagram };