⬆︎
×

[LC] 3019 Number of Changing Keys

Hyplus目录

Java

/**
 * <a href="https://leetcode.cn/problems/number-of-changing-keys/">Number of Changing Keys</a>
 * 字符串
 */
class Solution {
    public int countKeyChanges(String s) {
        int cnt = 0;
        for (int i = 1; i < s.length(); i++) {
            if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(i - 1))) {
                cnt++;
            }
        }
        return cnt;
    }
}

Go

import "unicode"

func countKeyChanges(s string) int {
    cnt := 0
    for i := 1; i < len(s); i++ {
        if unicode.ToLower(rune(s[i])) != unicode.ToLower(rune(s[i-1])) {
            cnt++
        }
    }
    return cnt
}

发表评论