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

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

题目要求

在这里插入图片描述

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

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

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

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(n)
  • 空间复杂度: O ( 1 ) 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(n)
  • 空间复杂度: O ( 1 ) O(1) O(1)

总结

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


欢迎指正与讨论!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值