FastPrepMinimum Changes for a Beautiful String

Minimum Changes for a Beautiful String

Microsoft logoMicrosoft● MediumINTERNOA
Learn

Problem statement

A lowercase string is beautiful when the absolute alphabet-index difference between every pair of adjacent characters is greater than 1. Equal letters and consecutive letters are therefore invalid neighbors; a and z are not consecutive.

In one operation, choose any position and replace its character with any lowercase English letter. Return the minimum number of operations needed to make s beautiful.

Function

getMinimumOperationCount(s: String) → int

Examples

Example 1

s = "bddje"return = 1

Changing the second d to f gives bdfje, whose adjacent differences are all greater than one.

Example 2

s = "acaz"return = 0

Every adjacent alphabet-index difference is already at least two.

Constraints

  • 1 <= s.length <= 100000.
  • s contains only lowercase English letters.

Source note: The source image is a source-neutral excerpt of the author-reported prompt.

More Microsoft problems

See Microsoft hiring insights
public int getMinimumOperationCount(String s) {
  // Write your code here.
}
s"bddje"
expected1
Checking account…