3019. Number of Changing Keys


  • You are given a 0-indexed string s typed by a user. Changing a key is defined as using a key different from the last used key. For example, s = "ab" has a change of a key while s = "bBBb" does not have any.
  • Return *the number of times the user had to change the key.**
  • *Note: Modifiers like shift or caps lock won’t be counted in changing the key that is if a user typed the letter 'a' and then the letter 'A' then it will not be considered as a changing of key.

Example 1

Input: s = "aAbBcC"
Output: 2
Explanation: 
From s[0] = 'a' to s[1] = 'A', there is no change of key as caps lock or shift is not counted.
From s[1] = 'A' to s[2] = 'b', there is a change of key.
From s[2] = 'b' to s[3] = 'B', there is no change of key as caps lock or shift is not counted.
From s[3] = 'B' to s[4] = 'c', there is a change of key.
From s[4] = 'c' to s[5] = 'C', there is no change of key as caps lock or shift is not counted.

Example 2

Input: s = "AaAaAaaA"
Output: 0
Explanation: There is no change of key since only the letters 'a' and 'A' are pressed which does not require change of key.

Method 1

【O(n) time | O(1) space】
package Leetcode.Array;

/**
 * @author zhengxingxing
 * @date 2025/01/07
 */
public class NumberOfChangingKeys {
    
    public static int countKeyChanges(String s) {
        if(s == null || s.length() <= 1){
            return 0;
        }

        int change = 0;
        // Convert first character to lowercase as initial comparison character
        char prev = Character.toLowerCase(s.charAt(0));

        // Iterate through each character in the string
        for (int i = 1; i < s.length(); i++) {
            // Convert current character to lowercase for comparison
            char curr = Character.toLowerCase(s.charAt(i));

            // If current character is different from previous, increment change counter
            if(curr != prev){
                change++;
            }
            prev = curr;
        }

        return change;
    }

    public static void main(String[] args) {
        // Test case 1
        String s1 = "aAbBcC";
        System.out.println("Test case 1: " + s1);
        System.out.println("Expected result: 2");
        System.out.println("Actual result: " + countKeyChanges(s1));

        // Test case 2
        String s2 = "AaAaAaaA";
        System.out.println("\nTest case 2: " + s2);
        System.out.println("Expected result: 0");
        System.out.println("Actual result: " + countKeyChanges(s2));

        // Additional test case
        String s3 = "mZiEPOz";
        System.out.println("\nTest case 3: " + s3);
        System.out.println("Actual result: " + countKeyChanges(s3));
    }
}



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • 2264. Largest 3-Same-Digit Number in String
  • 3042. Count Prefix and Suffix Pairs I
  • 1016. Binary String With Substrings Representing 1 To N
  • 1408. String Matching in an Array
  • 2269. Find the K-Beauty of a Number