Skip to content

3019. Number of Changing Keys 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  int countKeyChanges(string s) {
    int ans = 0;
    for (int i = 1; i < s.length(); ++i)
      if (tolower(s[i]) != tolower(s[i - 1]))
        ++ans;
    return ans;
  }
};
1
2
3
4
5
6
7
8
9
class Solution {
  public int countKeyChanges(String s) {
    int ans = 0;
    for (int i = 1; i < s.length(); ++i)
      if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(i - 1)))
        ++ans;
    return ans;
  }
}
1
2
3
4
class Solution:
  def countKeyChanges(self, s: str) -> int:
    return sum(a.lower() != b.lower()
               for a, b in itertools.pairwise(s))