【好】strong-password-checker,我自己做出来的:)

我自己做出来的,分了几种情况来考虑。(再后面有加了注释的版本)

https://leetcode.com/problems/strong-password-checker/

// 加油!

public class Solution {

    public int strongPasswordChecker(String s) {
        int sLen = s.length();
        if (sLen < 4) {
            return 6 - sLen;
        }
        int lnum = 1;
        int unum = 1;
        int dnum = 1;
        int rcount = 0;
        int ricount = 0;
        int rdcount = 0;
        int sameseq = 0;

        for (int i=0; i<sLen; i++) {
            char ch = s.charAt(i);
            if (ch>=‘a‘ && ch<=‘z‘) {
                lnum = 0;
            }
            if (ch>=‘A‘ && ch<=‘Z‘) {
                unum = 0;
            }
            if (ch>=‘0‘ && ch<=‘9‘) {
                dnum = 0;
            }

            // fix bug
            if (i == 0) {
                sameseq = 1;
            }
            else if (ch != s.charAt(i-1)) {
                if (sameseq >= 3) {
                    // 这个很重要
                    while (sLen + ricount < 6 && sameseq >= 3) {
                        ricount++;
                        sameseq -= 2;
                    }
                    while (sLen - rdcount > 20 && sameseq >= 3) {
                        rdcount++;
                        sameseq --;
                    }
                    rcount += sameseq / 3;
                }
                sameseq = 1;
            }
            else {
                sameseq++;
            }
        }

        // fixbug
        if (sameseq >= 3) {
            // 这个很重要
            while (sLen + ricount < 6 && sameseq >= 3) {
                ricount++;
                sameseq -= 2;
            }
            while (sLen - rdcount > 20 && sameseq >= 3) {
                rdcount++;
                sameseq --;
            }
            rcount += sameseq / 3;
        }

        //System.out.printf("rcount: %d, ricount: %d, rdcount: %d, lnum: %d, unum: %d, dnum: %d\n",
        //        rcount, ricount, rdcount, lnum, unum, dnum);

        int update = lnum + unum + dnum;
        int must = ricount + rcount;
        if (sLen + ricount < 6) {
            must += 6 - sLen - ricount;
        }
        if (sLen < 20) {
            return must > update ? must : update;
        }

        // 跟上面的不一样,因为删除字符是无法增加新的类型的
        if (sLen - rdcount > 20) {
            rdcount += sLen - rdcount - 20;
        }
        return rcount >= update ? rcount + rdcount : update + rdcount;

    }

}

以下是加了注释的版本:

public class Solution {

    public int strongPasswordChecker(String s) {
        int sLen = s.length();
        if (sLen < 4) {
            return 6 - sLen;
        }

        int lnum = 1; // need lower
        int unum = 1; // need upper
        int dnum = 1; // need digit

        int rcount = 0;  // count need to replace repeated seq
        int ricount = 0; // count need to add in repeated seq
        int rdcount = 0; // count need to remove from repeated seq
        int sameseq = 0; // count of chars in repeated seq

        for (int i=0; i<sLen; i++) {
            char ch = s.charAt(i);
            if (ch>=‘a‘ && ch<=‘z‘) {
                lnum = 0;
            }
            if (ch>=‘A‘ && ch<=‘Z‘) {
                unum = 0;
            }
            if (ch>=‘0‘ && ch<=‘9‘) {
                dnum = 0;
            }

            // check repeated seq
            if (i == 0) {
                sameseq = 1;
            }
            else if (ch != s.charAt(i-1)) {
                if (sameseq >= 3) {
                    // if shorter length, add char into repeated seq
                    while (sLen + ricount < 6 && sameseq >= 3) {
                        ricount++;
                        sameseq -= 2;
                    }
                    // if longer length, remove char from repeated seq
                    while (sLen - rdcount > 20 && sameseq >= 3) {
                        rdcount++;
                        sameseq --;
                    }
                    // if length matches, replace char in repeated seq
                    rcount += sameseq / 3;
                }
                sameseq = 1;
            }
            else {
                sameseq++;
            }
        }

        // need check repeated seq after loop
        if (sameseq >= 3) {
            // as previous process
            while (sLen + ricount < 6 && sameseq >= 3) {
                ricount++;
                sameseq -= 2;
            }
            while (sLen - rdcount > 20 && sameseq >= 3) {
                rdcount++;
                sameseq --;
            }
            rcount += sameseq / 3;
        }

        int update = lnum + unum + dnum;
        int must = ricount + rcount;
        if (sLen + ricount < 6) {
            must += 6 - sLen - ricount;
        }
        if (sLen < 20) {
            return must > update ? must : update;
        }

        // if longer length, use below process
        if (sLen - rdcount > 20) {
            rdcount += sLen - rdcount - 20;
        }
        return rcount >= update ? rcount + rdcount : update + rdcount;

    }

}

准备发表在Discuss版:

https://discuss.leetcode.com/category/549/strong-password-checker

时间: 2024-10-12 23:59:00

【好】strong-password-checker,我自己做出来的:)的相关文章

Leetcode: Strong Password Checker

A password is considered strong if below conditions are all met: It has at least 6 characters and at most 20 characters. It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit. It must NOT contain three r

[LeetCode] Strong Password Checker 密码强度检查器

A password is considered strong if below conditions are all met: It has at least 6 characters and at most 20 characters. It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit. It must NOT contain three r

420. Strong Password Checker

不定期更新leetcode解题java答案. 采用pick one的方式选择题目. 本题为密码检测程序,给定字符串,要求1.字符串长度在6~20之间,2.字符串至少包含小写字母.大写字母和数字,3.字符串不允许存在3个连续的相同字符.(如:"aaa") 问:需要几次变换操作可以将给定字符串改写为规范密码字符串.变换操作包括:1.插入一个字符,2.删除一个字符,3.修改一个字符. 首先考虑长度问题,如果小于6个或者大于20个字符,每多一个字符必须做出一次处理,如果少则插入:多则删除. 然

练练脑,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

继续过Hard题目.0209

http://www.cnblogs.com/charlesblc/p/6372971.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

继续过Hard题目

接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 L

练几道,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

BackTrack5 (BT5)无线password破解教程之WPA/WPA2-PSK型无线password破解

昨天公布了BackTrack5 (BT5)无线weppassword破解教程之minidwep-gtk破解法一文,对BT5下破解wep无线password的简单方法做了介绍,今天奶牛为朋友们介绍下怎样在bt5下破解wpa wpa2类型的无线password. 前提:安装或者硬盘引导了BT5的gnome32位镜像,能够參看奶牛的文章BackTrack5硬盘引导+BT5硬盘安装全教程 尽管人们都说wpa的password难破解,事实上,嗯,确实是这样子,不只靠技术,还要靠运气,这里就要用到一个强大的

用PHP做服务器接口客户端用http协议POST访问安全性一般怎么做

我的问题是,如果不做安全相关处理的话,一些可能改变数据库的操作可能会遭遇垃圾数据提交什么的,毕竟要找到这些信息只要找个http包就可以了 系统无用户登录 新手问题(从来没做过服务端开发),如果可以,给几个主流方法的链接,多谢 直观总结方法二: 1.请求头里带用户username和password,到服务器端做验证,通过才继续下边业务逻辑. 有点:防止了服务器端api被随意调用. 缺点:每次都交互用户名和密码,交互量大,且密码明文传输不安全. 2.第一次请求,要求username和password