File tree Expand file tree Collapse file tree 4 files changed +165
-0
lines changed
docs/hackerrank/interview_preparation_kit/string_manipulation
src/hackerrank/interview_preparation_kit/string_manipulation Expand file tree Collapse file tree 4 files changed +165
-0
lines changed Original file line number Diff line number Diff line change 1+ # [ Strings: Alternating Characters] ( https://www.hackerrank.com/challenges/alternating-characters )
2+
3+ - Difficulty: ` #easy `
4+ - Category: ` #ProblemSolvingBasic ` ` #strings `
5+
6+ You are given a string containing characters ` A ` and ` B ` only.
7+ Your task is to change it into a string such that there are
8+ no matching adjacent characters.
9+ To do this, you are allowed to delete zero or more
10+ characters in the string.
11+
12+ Your task is to find the minimum number of required deletions.
13+
14+ ## Example
15+
16+ ` s = AABAAB `
17+
18+ Remove an at ` A ` positions ` 0 ` and ` 3 ` to make ` s = ABAB ` in ` 2 ` deletions.
19+
20+ ## Function Description
21+
22+ Complete the alternatingCharacters function in the editor below.
23+ alternatingCharacters has the following parameter(s):
24+
25+ - ` string s ` : a string
26+
27+ ## Returns
28+
29+ - ` int ` : the minimum number of deletions required
30+
31+ ## Input Format
32+
33+ The first line contains an integer ` q ` , the number of queries.
34+ The next ` q ` lines each contain a string ` s ` to analyze.
35+
36+ ## Constraints
37+
38+ - $ 1 \leq q \leq 10 $
39+ - $ 1 \leq lenght of s \leq 10^5 $
40+ - Each string ` s ` will consist only of characters ` A ` and ` B ` .
41+
42+ ## Sample Input
43+
44+ ``` text
45+ 5
46+ AAAA
47+ BBBBB
48+ ABABABAB
49+ BABABA
50+ AAABBB
51+ ```
52+
53+ ## Sample Output
54+
55+ ``` text
56+ 3
57+ 4
58+ 0
59+ 0
60+ 4
61+ ```
62+
63+ ## Explanation
64+
65+ The characters marked red are the ones that can be deleted
66+ so that the string does not have matching adjacent characters.
67+
68+ ``` text
69+ AAAA => A, 3 deletions
70+ BBBBB => B, 4 deletions
71+ ABABABAB => ABABABAB, 0 deletions
72+ BABABA => BABABA, 0 deletions
73+ AAABBB => AB, 4 deletions
74+ ```
Original file line number Diff line number Diff line change 1+ import { describe , expect , it } from '@jest/globals' ;
2+
3+ import { alternatingCharacters } from './alternating_characters' ;
4+ import TEST_CASES from './alternating_characters.testcases.json' ;
5+
6+ describe ( 'alternatingCharacters' , ( ) => {
7+ it ( 'alternatingCharacters test cases' , ( ) => {
8+ expect . assertions ( 9 ) ;
9+
10+ TEST_CASES . forEach ( ( testSet ) => {
11+ testSet ?. tests . forEach ( ( test ) => {
12+ const result = alternatingCharacters ( test . input ) ;
13+
14+ expect ( result ) . toStrictEqual ( test . expected ) ;
15+ } ) ;
16+ } ) ;
17+ } ) ;
18+ } ) ;
Original file line number Diff line number Diff line change 1+ [
2+ {
3+ "title" : " Sample Test case 0" ,
4+ "tests" : [
5+ {
6+ "input" : " AAAA" ,
7+ "expected" : 3
8+ },
9+ {
10+ "input" : " BBBBB" ,
11+ "expected" : 4
12+ },
13+ {
14+ "input" : " ABABABAB" ,
15+ "expected" : 0
16+ },
17+ {
18+ "input" : " BABABA" ,
19+ "expected" : 0
20+ },
21+ {
22+ "input" : " AAABBB" ,
23+ "expected" : 4
24+ }
25+ ]
26+ },
27+ {
28+ "title" : " Sample Test case 13" ,
29+ "tests" : [
30+ {
31+ "input" : " AAABBBAABB" ,
32+ "expected" : 6
33+ },
34+ {
35+ "input" : " AABBAABB" ,
36+ "expected" : 4
37+ },
38+ {
39+ "input" : " ABABABAA" ,
40+ "expected" : 1
41+ }
42+ ]
43+ },
44+ {
45+ "title" : " Sample Test case 14" ,
46+ "tests" : [
47+ {
48+ "input" : " ABBABBAA" ,
49+ "expected" : 3
50+ }
51+ ]
52+ }
53+ ]
Original file line number Diff line number Diff line change 1+ /**
2+ * @link Problem definition [[docs/hackerrank/interview_preparation_kit/string_manipulation/alternating-characters.md]]
3+ */
4+
5+ export function alternatingCharacters ( s : string ) : number {
6+ let last = '' ;
7+ let newString = '' ;
8+
9+ for ( const letter of s ) {
10+ if ( letter !== last ) {
11+ newString += letter ;
12+
13+ last = letter ;
14+ }
15+ }
16+
17+ return s . length - newString . length ;
18+ }
19+
20+ export default { alternatingCharacters } ;
You can’t perform that action at this time.
0 commit comments