Skip to content

字符串

最小化字符串长度

2716. 最小化字符串长度 - 力扣(LeetCode)

个人做法:使用数组统计每个字符数量,如果为遍历到的字符串在数组中存储的还是0的话结果加1(说明是第一次遍历到该字符)

java
class Solution {
    public int minimizedStringLength(String s) {
        int[] arr = new int[26];
        int res = 0;
        for(int i = 0;i < s.length();i++){
            if(arr[s.charAt(i) - 'a']++ == 0){
                res++;
            }
        }
        return res;
    }
}

优化:使用

java