Get Minimum Operation Count
Learn this problemProblem statement
A string is beautiful if no two adjacent characters are either
The following operations can be performed on a string, s.
i (0 ≤ i < |s|) and change s[i] to any lowercase English letter.Find the minimum number of operations required to make the string beautiful.
Function
getMinimumOperationCount(s: String) → int
Complete the function getMinimumOperationCount in the editor below.
getMinimumOperationCount has the following parameter:
s: a string
Returns
int: the minimum number of operations required to make s beautiful
Examples
Example 1
s = "abdde"return = 2
String s is not beautiful because:
- 'dd' violates constraint 1, no two adjacent characters are the same.
- 'ab' and 'de' violate constraint 2, no two adjacent characters are adjacent in the alphabet.
The string can be converted into a beautiful string after 2 operations. One solution is below:
- Choose
i=1and changes[i]to 'z',sbecomes "azdde". - Choose
i=3and changes[i]to 'k',sbecomes "azdke" which is beautiful.
Note: There are many other solutions such as "ardze", "axdke", etc.
It can be shown that 2 is the minimum number of operations required so return 2.
Constraints
- 2 ≤ |
s| ≤ 10^5 - The string
scontains only lowercase English letters.
More IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026