Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# [Strings: Alternating Characters](https://www.hackerrank.com/challenges/alternating-characters)

- Difficulty: `#easy`
- Category: `#ProblemSolvingBasic` `#strings`

You are given a string containing characters `A` and `B` only.
Your task is to change it into a string such that there are
no matching adjacent characters.
To do this, you are allowed to delete zero or more
characters in the string.

Your task is to find the minimum number of required deletions.

## Example

`s = AABAAB`

Remove an at `A` positions `0` and `3` to make `s = ABAB` in `2` deletions.

## Function Description

Complete the alternatingCharacters function in the editor below.
alternatingCharacters has the following parameter(s):

- `string s`: a string

## Returns

- `int`: the minimum number of deletions required

## Input Format

The first line contains an integer `q`, the number of queries.
The next `q` lines each contain a string `s` to analyze.

## Constraints

- $ 1 \leq q \leq 10 $
- $ 1 \leq lenght of s \leq 10^5 $
- Each string `s` will consist only of characters `A` and `B`.

## Sample Input

```text
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
```

## Sample Output

```text
3
4
0
0
4
```

## Explanation

The characters marked red are the ones that can be deleted
so that the string does not have matching adjacent characters.

```text
AAAA => A, 3 deletions
BBBBB => B, 4 deletions
ABABABAB => ABABABAB, 0 deletions
BABABA => BABABA, 0 deletions
AAABBB => AB, 4 deletions
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from '@jest/globals';

import { alternatingCharacters } from './alternating_characters';
import TEST_CASES from './alternating_characters.testcases.json';

describe('alternatingCharacters', () => {
it('alternatingCharacters test cases', () => {
expect.assertions(9);

TEST_CASES.forEach((testSet) => {
testSet?.tests.forEach((test) => {
const result = alternatingCharacters(test.input);

expect(result).toStrictEqual(test.expected);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[
{
"title": "Sample Test case 0",
"tests": [
{
"input": "AAAA",
"expected": 3
},
{
"input": "BBBBB",
"expected": 4
},
{
"input": "ABABABAB",
"expected": 0
},
{
"input": "BABABA",
"expected": 0
},
{
"input": "AAABBB",
"expected": 4
}
]
},
{
"title": "Sample Test case 13",
"tests": [
{
"input": "AAABBBAABB",
"expected": 6
},
{
"input": "AABBAABB",
"expected": 4
},
{
"input": "ABABABAA",
"expected": 1
}
]
},
{
"title": "Sample Test case 14",
"tests": [
{
"input": "ABBABBAA",
"expected": 3
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/string_manipulation/alternating-characters.md]]
*/

export function alternatingCharacters(s: string): number {
let last = '';
let newString = '';

for (const letter of s) {
if (letter !== last) {
newString += letter;

last = letter;
}
}

return s.length - newString.length;
}

export default { alternatingCharacters };