Skip to content

Commit 1b4a2e7

Browse files
authored
Merge pull request #519 from sir-gon/feature/ctci_making_anagrams
[Hacker Rank] Interview Preparation Kit: String Manipulation: Making …
2 parents fe86ea1 + 0463661 commit 1b4a2e7

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [Strings: Making Anagrams](https://www.hackerrank.com/challenges/ctci-making-anagrams)
2+
3+
How many characters should one delete to make two given strings anagrams
4+
of each other?
5+
6+
- Difficulty: `#easy`
7+
- Category: `#ProblemSolvingBasic` `#strings`
8+
9+
A student is taking a cryptography class and has found anagrams to be very useful.
10+
Two strings are anagrams of each other if the first
11+
string's letters can be rearranged to form the second string.
12+
In other words, both strings must contain the same
13+
exact letters in the same exact frequency.
14+
For example, `bacdc` and `dcbac` are anagrams,
15+
but `bacdc` and `dcbad` are not.
16+
17+
The student decides on an encryption scheme that involves two large strings.
18+
The encryption is dependent on the minimum number of character deletions
19+
required to make the two strings anagrams. Determine this number.
20+
21+
Given two strings, `a` and `b`, that may or may not be of the same length,
22+
determine the minimum number of character deletions required
23+
to make `a` and `b` anagrams.
24+
Any characters can be deleted from either of the strings.
25+
26+
## Example
27+
28+
```python
29+
a = 'cde'
30+
b = 'dcf'
31+
```
32+
33+
Delete `e` from `a` and `f` from `b` so that the remaining strings
34+
are `cd` and `dc` which are anagrams. This takes `2` character deletions.
35+
36+
## Function Description
37+
38+
Complete the makeAnagram function in the editor below.
39+
40+
makeAnagram has the following parameter(s):
41+
42+
- `string a`: a string
43+
- `string b`: another string
44+
45+
## Returns
46+
47+
- `int`: the minimum total characters that must be deleted
48+
49+
## Input Format
50+
51+
The first line contains a single string, `a`.
52+
The second line contains a single string, `b`.
53+
54+
## Constraints
55+
56+
- $ 1 \leq |a|, |b| \leq 10^4 $
57+
- The strings `a` and `b` consist of lowercase English alphabetic letters, ascii[a-z].
58+
59+
## Sample Input
60+
61+
```text
62+
cde
63+
abc
64+
```
65+
66+
## Sample Output
67+
68+
```text
69+
4
70+
```
71+
72+
## Explanation
73+
74+
Delete the following characters from the strings make them anagrams:
75+
76+
1. Remove `d` and `e` from `cde` to get `c`.
77+
2. Remove `a` and `b` from `abc` to get `c`.
78+
79+
It takes `4` deletions to make both strings anagrams.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/string_manipulation/ctci-making-anagrams.md]]
3+
*/
4+
5+
function charToDicMap(word) {
6+
const output = {};
7+
8+
for (const letter of word.split('')) {
9+
output[letter] = output?.[letter] ? output[letter] + 1 : 1;
10+
}
11+
12+
return output;
13+
}
14+
15+
function sum(values) {
16+
return values.reduce(
17+
(previousValue, currentValue) => previousValue + currentValue,
18+
0
19+
);
20+
}
21+
22+
export function makeAnagram(a, b) {
23+
const aMap = charToDicMap(a);
24+
const bMap = charToDicMap(b);
25+
26+
for (const key of Object.keys(aMap)) {
27+
if (bMap?.[key]) {
28+
aMap[key] = Math.abs(aMap[key] - bMap[key]);
29+
bMap[key] = 0;
30+
}
31+
}
32+
33+
return sum(Object.values(aMap)) + sum(Object.values(bMap));
34+
}
35+
36+
export default { makeAnagram };
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
3+
import { makeAnagram } from './ctci_making_anagrams.js';
4+
import TEST_CASES from './ctci_making_anagrams.testcases.json';
5+
6+
describe('makeAnagram', () => {
7+
it('makeAnagram test cases', () => {
8+
expect.assertions(3);
9+
10+
TEST_CASES.forEach((test) => {
11+
const result = makeAnagram(test.a, test.b);
12+
13+
expect(result).toStrictEqual(test.expected);
14+
});
15+
});
16+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"a": "cde",
5+
"b": "abc",
6+
"expected": 4
7+
},
8+
{
9+
"title": "Sample Test case 1",
10+
"a": "fcrxzwscanmligyxyvym",
11+
"b": "jxwtrhvujlmrpdoqbisbwhmgpmeoke",
12+
"expected": 30
13+
},
14+
{
15+
"title": "Sample Test case 2",
16+
"a": "showman",
17+
"b": "woman",
18+
"expected": 2
19+
}
20+
]

0 commit comments

Comments
 (0)