For each word in a list of words, if any two adjacent characters are equal, change one…
- User
For each word in a list of words, if any two adjacent characters are equal, change one of them. Determine the minimum number of substitutions so the final string contains no adjacent equal characters.
Example
words = ['add', 'boook', 'break']
-
'add': change one d (1 change)
-
'boook': change the middle o (1 change)
-
'break': no changes are necessary (0 changes)
The return array is [1,1,0].
Function Description
Complete the function minimal Operations in the editor below.
minimalOperations has the following parameter(s): string words[n]: an array of strings
Returns:
int[n]: each element is the minimum substitutions for words[i]
Constraints
1≤n≤100
• 2 ≤ length of words[i] < 105
• Each character of words/ilis in the range asciila-z).
▼Input Format for Custom Testing
Input from stdin will be processed as follows and passed to the function.
The first line contains an integer n, the size of the array words. Each of the next n lines contains a string words[i].
▼ Sample Case 0
Sample Input 0
STDIN
Function Parameters
5 ab → words [] Size = 5 → words[] = [ 'ab', 'aab', 'abb', 'abab', 'abaaaba']
aab
abb
abab
Sample Output 0
0
1
1
0
1
Explanation 0
words = 'ab' is already acceptable, so O replacements are needed.
words = 'aab' Replace an 'a'with an appropriate character so 1 replacement.
words = 'abb' is not acceptable. Replace a 'b'with an appropriate character, again 1 replacement.
⚫ words = 'abab' is already acceptable so O replacements are
needed.
words = 'abaaaba' is not acceptable. Replace the middle 'a' in 'aaa', 1 replacement.
-
- Assistant