Number of Changing Keys Problem in Java
Given a user-typed string with a 0-index. Using a key that is different from the one you just used to be known as changing a key. Give back how many times the user needed to swap out the key.
Note: Changes made to modifiers like shift or caps lock will not be considered when determining whether a user changed a key. For example, typing the letter 'a' followed by the letter 'A' would not be regarded as changing a key.
Example 1:
Input:
String str = "abaAbBBCc"
Output:
The number of keys that have to be changed is 4
Explanation:
For the given string "abaAbBBCc",
There is a key change from s[0] = "a" to s[1] = "b".
There is a key change from s[1] = "b" to s[2] = "a".
There is no key change from s[2] = "a" to s[3] = "A" since shift or caps lock are not counted.
There is a key change from s[3] = "A" to s[4] = "b".
There is no key change from s[4] = "b" to s[5] = "B" since shift or caps lock are not counted.
There is no key change from s[5] = "B" to s[6] = "B" since shift or caps lock are not counted.
There is a key change from s[6] = "B" to s[7] = "C".
There is no key change from s[7] = "C" to s[8] = "c" since shift or caps lock are not counted.
Hence total number of keys that are to be changed is 4.
Example 2:
Input:
String str = "BBBB"
Output:
The number of keys that have to be changed is 0
Explanation:
Since all the characters are similar and are in the form of shift or caps locks, which are not counted, the number of keys changed is 0.
Approach: Using toLowercase() Method
In a given string s, the method counts the number of necessary key changes. For consistency between uppercase and lowercase characters, the string is converted to lowercase. It then compares next to characters as it moves along the string. It increases the count each time it finds a transition between two or more characters. The count that indicates the total number of key modifications required is finally returned by it.
Algorithm:
Step 1: Initialise a variable count to zero to keep track of the number of keys that are to be changed.
Step 2: To ensure case insensitivity, convert the input string, str, to lowercase.
Step 3: Utilising a for loop, go through the string's characters beginning at index 1.
Step 4: Compare the current character with the prior character within the loop. Increase the count variable if they are different.
Step 5: Return the value of the count after iterating through the full string.
Implementation:
FileName: ChangingKeys.java
import java.io.*; import java.util.*; public class ChangingKeys { public int KeyChangesCount(String str) { // To count the various characters, use the count variable. int count = 0; //Because small and capital letters are the same, transform the string to lower case str = str.toLowerCase(); for(int i = 1; i{ // If more than one character appears, //The count value will increase by one. if(str.charAt(i-1) != str.charAt(i)) { count++; } } return count; } public static void main(String[] args) { ChangingKeys obj = new ChangingKeys(); String str = "abaAbBBCc"; int result = obj.countKeyChanges(str); System.out.println("The number of keys that has to be changed are " + obj.countKeyChanges(str)); } }
Output:
The number of keys that have to be changed is 4
Complexity Analysis:
The time complexity is O(N), and the space complexity is O(1).