HackerRank içerisinde bulunan “Alternating Characters” sorusunun açıklaması ve çözümü. Soruda size verilen string içerisinde, yan yana hiçbir karakterin aynı olmaması için kaç defa karakter silme işlemi yapmanız gerektiği soruluyor.
► HackerRank – Alternating Characters: https://www.hackerrank.com/challenges/alternating-characters/problem
► Problem açıklaması:
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 A at 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
Sample Input
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
Sample Output
3
4
0
0
4