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

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

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

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

    A password is considered strong if below conditions are all met: It has at least 6 characters and at ...

  2. Leetcode: Strong Password Checker

    A password is considered strong if below conditions are all met: It has at least 6 characters and at ...

  3. [Swift]LeetCode420. 强密码检验器 | Strong Password Checker

    A password is considered strong if below conditions are all met: It has at least 6 characters and at ...

  4. Hard模式题目

    先过一下Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Ha ...

  5. 练练脑,继续过Hard题目

    http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Diffi ...

  6. leetcode 学习心得 (2) (301~516)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  10. 继续过Hard题目

    接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance ...

随机推荐

  1. Mybatis学习—XML映射文件

    总结自 Mybatis官方中文文档 Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同 ...

  2. redis之(十一)redis实现缓存的功能

    [一]redis实现缓存的原理 --->利用键的失效时间设置实现缓存技术 --->由于redis的内存有限,可以在redis的配置文件里设置maxmemory的参数.来限制redis最大可 ...

  3. PHP7.3发布啦

    作为PHP5的最后一个版本,也是目前使用最广泛的PHP版本,PHP 5.6始于公元2014年(不是1804年,嘿嘿),其第一个测试版PHP 5.6 alpha 1版于2014年1月发布.随机产生了第一 ...

  4. 递归遍历JSON树

    递归遍历JSON树 前几天有个人问我,json串的层级无限深,但在json串中的key是已知的,在json串中的value,有些事Object,有些是Array,如何把这些层级无限深的key所对应的v ...

  5. nginx 开启 gzip

    gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_comp_level 2; gzip_types text/plain applicatio ...

  6. nginx gzip压缩

    gzip压缩作用:将响应报⽂发送⾄客户端之前可以启⽤压缩功能,这能够有效地节约带宽,并提⾼响应⾄客户端的速度,压缩会消耗nginx的cpu性能 gzip压缩可以配置http,server和locati ...

  7. mysql ERROR 1366

    mysql ERROR 1366 mysql> INSERT INTO tb_room VALUES ('9101','9','1',300,'9101',0,1,'超级豪华间','public ...

  8. magento批量上传产品

    Step1:表格仔细检查无误后,将准备好的图片上传至 media/import中.如果使用专用的图片服务器,把图片上传到服务器上,当然表格中的图片地址要做相应的修改. Step2:然后,登陆Magen ...

  9. 转:Google Project Zero挖洞经验整理

    https://www.sec-un.org/google-project-zero%E6%8C%96%E6%B4%9E%E7%BB%8F%E9%AA%8C%E6%95%B4%E7%90%86/ 1. ...

  10. Flume学习应用:Java写日志数据到MongoDB

    概述 Windows平台:Java写日志到Flume,Flume最终把日志写到MongoDB. 系统环境 操作系统:win7 64 JDK:1.6.0_43 资源下载 Maven:3.3.3下载.安装 ...