LeetCode-in-Java

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

public class Solution {
    public int minOperations(String s) {
        int n = s.length();
        int ans = 0;
        for (int i = 0; i < n; i++) {
            final char c = s.charAt(i);
            if (c != 'a') {
                int ops = 'z' - c + 1;
                if (ops > ans) {
                    ans = ops;
                }
                if (ops == 25) {
                    break;
                }
            }
        }
        return ans;
    }
}