LeetCode in Kotlin

3675. Minimum Operations to Transform String

Medium

You are given a string s consisting only of lowercase English letters.

You can perform the following operation any number of times (including zero):

Return the minimum number of operations required to transform s into a string consisting of only 'a' characters.

Note: Consider the alphabet as circular, thus 'a' comes after 'z'.

Example 1:

Input: s = “yz”

Output: 2

Explanation:

Example 2:

Input: s = “a”

Output: 0

Explanation:

Constraints:

Solution

class Solution {
    fun minOperations(s: String): Int {
        val n = s.length
        var ans = 0
        for (i in 0..<n) {
            val c = s[i]
            if (c != 'a') {
                val ops = 'z'.code - c.code + 1
                if (ops > ans) {
                    ans = ops
                }
                if (ops == 25) {
                    break
                }
            }
        }
        return ans
    }
}