Skip to content

Commit

Permalink
[Hacker Rank] Interview Preparation Kit: String Manipulation: Making …
Browse files Browse the repository at this point in the history
…Anagrams. Solved ✅.
  • Loading branch information
Gonzalo Diaz committed Aug 30, 2024
1 parent 5b4d5cf commit e88ba7c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Original file line number Diff line number Diff line change
@@ -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
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/string_manipulation/m]]
*/

function charToDicMap(word: string): Record<string, number> {
const output: Record<string, number> = {};

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 };

0 comments on commit e88ba7c

Please sign in to comment.