diff --git a/docs/hackerrank/interview_preparation_kit/string_manipulation/alternating-characters.md b/docs/hackerrank/interview_preparation_kit/string_manipulation/alternating-characters.md new file mode 100644 index 00000000..adcfed17 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/string_manipulation/alternating-characters.md @@ -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 +``` diff --git a/src/hackerrank/interview_preparation_kit/string_manipulation/alternating_characters.test.ts b/src/hackerrank/interview_preparation_kit/string_manipulation/alternating_characters.test.ts new file mode 100644 index 00000000..88d604b8 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/string_manipulation/alternating_characters.test.ts @@ -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); + }); + }); + }); +}); diff --git a/src/hackerrank/interview_preparation_kit/string_manipulation/alternating_characters.testcases.json b/src/hackerrank/interview_preparation_kit/string_manipulation/alternating_characters.testcases.json new file mode 100644 index 00000000..38ee7b2e --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/string_manipulation/alternating_characters.testcases.json @@ -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 + } + ] + } +] diff --git a/src/hackerrank/interview_preparation_kit/string_manipulation/alternating_characters.ts b/src/hackerrank/interview_preparation_kit/string_manipulation/alternating_characters.ts new file mode 100644 index 00000000..6f3c430a --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/string_manipulation/alternating_characters.ts @@ -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 };