Java&C++题解与拓展——leetcode420.强密码检验器【“困难”模拟题】

609 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

每日一题做题记录,参考官方和三叶的题解

题目要求

在这里插入图片描述

思路:模拟w(゚Д゚)w

根据密码长度直接分情况讨论:

  • n<6n<6:长度不够,要增加长度所以用到增加操作,对三连串需要进行替换(删除+增加),最少操作次数就是单纯加够长度or种类,即max(6n,3typ)max(6-n,3-typ)
  • 6n206\le n \le 20:要破坏三连串,最佳的操作是进行替换,最少次数是当前三连串的长度除以三,即leni3\lfloor \frac{len_i}{3} \rfloor,最少操作次数就是消掉所有三连串或加(换)够种类,即max(leni3,3typ)max(\sum\lfloor \frac{len_i}{3} \rfloor, 3 - typ)
  • n>20n>20:要先把长度调整到合适长度,需要base=n20base=n-20删除操作,然后用替换破坏三连串,由于之前的删除操作可以破坏掉一定的三连串,具体规律是每删除三个字符就可以减少依次替换,那么根据lenlen33的值根据从小到大进行更新统计得到需替换操作数tottot,最少操作次数就是调整至合适长度后消掉所有三连串或加(换)够种类,即base+max(tot,3m)base+max(tot,3-m)

Java

class Solution {
    public int strongPasswordChecker(String password) {
        char[] cs = password.toCharArray();
        int n = cs.length;
        int num = 0, low = 0, cap = 0;
        for(char c : cs) {
            if(c >= '0' && c <= '9')
                num = 1;
            else if(c >= 'a' && c <= 'z')
                low = 1;
            else if(c >= 'A' && c <= 'Z')
                cap = 1;
        }
        int typ = num + low + cap; //字符种类
        if(n < 6) { //增加、替换
            return Math.max(6 - n, 3 - typ);
        }
        else if(n <= 20) { //替换
            int tot = 0;
            for(int i = 0; i < n; ) { //统计三连串
                int j = i;
                while(j < n && cs[j] == cs[i])
                    j++;
                int len = j - i;
                if(len >= 3)
                    tot += len / 3;
                i = j;
            }
            return Math.max(tot, 3 - typ);
        }
        else {
            int tot = 0;
            int[] cnts = new int[3]; //三连串长度余数为i的数量
            for(int i = 0; i < n; ) { //统计三连串
                int j = i;
                while(j < n && cs[j] == cs[i])
                    j++;
                int len = j - i;
                if(len >= 3) {
                    tot += len / 3;
                    cnts[len % 3]++;
                }
                i = j;
            }
            int base = n - 20, cur = base; //至少需“删除”
            for(int i = 0; i < 3; i++) { //更新三连串,统计“替换”
                if(i == 2)
                    cnts[i] = tot;
                if(cnts[i] != 0 && cur != 0) {
                    int t = Math.min(cnts[i] * (i + 1), cur);
                    cur -= t;
                    tot -= t / (i + 1);
                }
            }
            return base + Math.max(tot, 3 - typ);
        }
    }
}
  • 时间复杂度:O(n)O(n)
  • 空间复杂度:O(1)O(1)

C++

class Solution {
public:
    int strongPasswordChecker(string password) {
        int n = password.size();
        int num = 0, low = 0, cap = 0;
        for(char c : password) {
            if(c >= '0' && c <= '9')
                num = 1;
            else if(c >= 'a' && c <= 'z')
                low = 1;
            else if(c >= 'A' && c <= 'Z')
                cap = 1;
        }
        int typ = num + low + cap; //字符种类
        if(n < 6) { //增加、替换
            return max(6 - n, 3 - typ);
        }
        else if(n <= 20) { //替换
            int tot = 0;
            for(int i = 0; i < n; ) { //统计三连串
                int j = i;
                while(j < n && password[j] == password[i])
                    j++;
                int len = j - i;
                if(len >= 3)
                    tot += len / 3;
                i = j;
            }
            return max(tot, 3 - typ);
        }
        else {
            int tot = 0;
            int cnts[3]{0}; //三连串长度余数为i的数量,记得初始化
            for(int i = 0; i < n; ) { //统计三连串
                int j = i;
                while(j < n && password[j] == password[i])
                    j++;
                int len = j - i;
                if(len >= 3) {
                    tot += len / 3;
                    cnts[len % 3]++;
                }
                i = j;
            }
            int base = n - 20, cur = base; //至少需“删除”
            for(int i = 0; i < 3; i++) { //更新三连串,统计“替换”
                if(i == 2)
                    cnts[i] = tot;
                if(cnts[i] != 0 && cur != 0) {
                    int t = min(cnts[i] * (i + 1), cur);
                    cur -= t;
                    tot -= t / (i + 1);
                }
            }
            return base + max(tot, 3 - typ);
        }
    }
};
  • 时间复杂度:O(n)O(n)
  • 空间复杂度:O(1)O(1)

总结

一个困难到没有模拟之外方法的题,它难就难在t……不要考虑太多太复杂,什么贪心什么动态规划,都不要,暴力模拟就完了🙂。
今日收获:我的各平台密码都是强密码呢【骄傲(‾◡◝)】


欢迎指正与讨论!