420. 强密码检验器

一个强密码应满足以下所有条件:

由至少6个,至多20个字符组成。

至少包含一个小写字母,一个大写字母,和一个数字。

同一字符不能连续出现三次 (比如 “…aaa…” 是不允许的, 但是 “…aa…a…” 是可以的)。

编写函数 strongPasswordChecker(s),s 代表输入字符串,如果 s 已经符合强密码条件,则返回0;否则返回要将 s 修改为满足强密码条件的字符串所需要进行修改的最小步数。

插入、删除、替换任一字符都算作一次修改。

PS:

这题真烦人

举例 aaaaa 五连字符,要正确的话如果只删除要 33 步,
如果插入的话要 22 步,如果替换只需要替换中间的 aa 一步就可以完成。
长度 <6 ,步数=缺失类型和缺失长度取大者。
长度 (6,20),这时候我们不需要低效的插入和删除来处理连续字符,直接替换步数就等于处理连续字和缺失类型取大者。
比较负载的是 >20,我们需要知道优先级,一样优先处理连续数组。
优先处理缺失类型,用替换的方式来处理,这时候要替换的连续组的连续数 %3==2 -> 连续数%3==1 -> 连续数%3==0,然后处理多余字符,删除的优先级是连续组的连续数 %3==0 -> 连续数%3==1 -> 连续数%3==2
class Solution {
/**
* 记录连续出现的字符 起始和终止坐标
*/
class SameChar {
int st;
int en;
char c; SameChar(int st, int en, char c) {
this.st = st;
this.en = en;
this.c = c;
} } public int strongPasswordChecker(String str) {
// 统计小写字符
int lowerCase = 0;
// 统计大写字符
int upwerCase = 0;
// 统计数字
int number = 0;
// 统计连续字符出现的位置
java.util.ArrayList<SameChar> sameChars = new java.util.ArrayList<SameChar>();
char[] chars = str.toCharArray();
if (chars.length == 0) {
return 6;
}
// 记露连续出现的字符
SameChar sameChar = new SameChar(0, 0, '\0');
for (int i = 0; i < chars.length; i++) {
if (chars[i] >= 'a' && chars[i] <= 'z') {
lowerCase++;
} else if (chars[i] >= 'A' && chars[i] <= 'Z') {
upwerCase++;
} else if (chars[i] >= '0' && chars[i] <= '9') {
number++;
}
if (sameChar.c != chars[i]) {
if (sameChar.en - sameChar.st >= 2) {
sameChars.add(new SameChar(sameChar.st, sameChar.en, sameChar.c));
}
sameChar.c = chars[i];
sameChar.st = i;
sameChar.en = i;
} else {
sameChar.en = i;
}
}
if (sameChar.en - sameChar.st >= 2) {
sameChars.add(new SameChar(sameChar.st, sameChar.en, sameChar.c));
}
// 缺失的类型. 只可能是1 or 2
int needType = count0(lowerCase, upwerCase, number);
// 连续的字符出现的要消除的个数 连续值-2
int[] chages = new int[sameChars.size()];
for (int j = 0; j < sameChars.size(); j++) {
chages[j] = sameChars.get(j).en - sameChars.get(j).st - 1;
}
int res = 0;
// 如果长度小于6 , 很简单 要补的字符和缺失的类型择大
if (str.length() < 6) {
return Integer.max(6 - str.length(), needType);
}
// 删除的时候 要有优先概念
if (str.length() > 20) {
int index = -1;
while (needType > 0 && (index = find(chages, 0)) > -1) {
chages[index] = Integer.max(chages[index] - 3, 0);
res++;
needType--;
}
int d = str.length() - 20;
while (d > 0 && (index = find(chages, 1)) > -1) {
d--;
res++;
chages[index]--;
}
int n = 0;
for (int l = 0; l < chages.length; l++) {
n += chages[l] % 3 == 0 ? chages[l] / 3 : chages[l] / 3 + 1;
}
return res + d + needType + n;
}
int n = 0;
for (int l = 0; l < chages.length; l++) {
n += chages[l] % 3 == 0 ? chages[l] / 3 : chages[l] / 3 + 1;
}
return Integer.max(n, needType);
} private int count0(int... array) {
int n = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == 0) {
n++;
}
}
return n;
} private int find(int[] array, int n) {
int n0 = -1;
int n1 = -1;
int n2 = -1;
for (int i = 0; i < array.length; i++) {
if (array[i] > 0 && array[i] % 3 == 0) {
n0 = i;
}
if (array[i] > 0 && array[i] % 3 == 1) {
n1 = i;
}
if (array[i] > 0 && array[i] % 3 == 2) {
n2 = i;
}
}
if (n == 0) {
return n0 > -1 ? n0 : (n2 > -1 ? n2 : n1);
}
if (n == 1) {
return n1 > -1 ? n1 : (n2 > -1 ? n2 : n0);
}
return -1;
} }

Java实现 LeetCode 420 强密码检验器的更多相关文章

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

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

  2. Java实现7种常见密码算法

    原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介 前面在密码学入门一文中讲解了各种常见的密码学概念.算法与运用场景,但没有介绍过代码,因此,为作补充,这一篇将会介绍 ...

  3. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. mysql root强密码的必要性max_allowed_packet被改成1024引起的风险

    前两天运维反馈说,有些机器的max_allowed_packet隔两天就会被改成1024,导致客户端调用时出错,网上有说内存不够的,也有人工修改的. 运维小姑娘一口咬定肯定没有改过的,而且my.cnf ...

  9. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. Jekyll 解决Jekyll server本地预览文章not found的问题

    layout: post tags: [Jekyll] comments: true 执行Jekyll本地浏览器预览指令 bundle exec jekyll serve 进入浏览器输入127.0.0 ...

  2. 单片机之静态局部变量static

    HL-1慧静电子 上程序: main.c #include <reg52.h>#include "Timer.h" /********P1口低有效*********** ...

  3. Java设计模式之建造者模式(Builder Pattern)

    前言 这篇文章主要向大家讲解什么是建造者模式,建造者模式的实例讲解及应用场景等知识点. 一.建造者介绍 ​ 用户可以不知道产品的构建细节直接可以创建复杂的对象,主要是分离了产品的构建和装配,这样就实现 ...

  4. python之导入模块的方法

    一.导入标准库模块 1.第一种方式: 可以通过以下方法导入 import time 当使用时间模块的sleep方法时可以使用 time.sleep(2) 2.第二种方式: 当只想使用sleep函数时, ...

  5. 比AtomicLong更优秀的LongAdder确定不来了解一下吗?

    前言 思维导图.png 文章中所有高清无码图片在公众号号回复: 图片666 即可查阅, 可直接关注公众号:壹枝花算不算浪漫 最近阿里巴巴发布了Java开发手册(泰山版) (公众号回复: 开发手册 可收 ...

  6. HDU 2001 (水)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2001 题目大意:两个点求距离 解题思路: 套基本公式 a = √(b2 + c2); 小数点后几位的表 ...

  7. 如何使用 frp 实现内网穿透

    这有一个专注Gopher技术成长的开源项目「go home」 背景 作为一名程序员,家里多多少少会有一些落了灰的电脑,如果把闲置的电脑变成服务器,不仅有良好的配置,还能用来做各种测试,那就再好不过了. ...

  8. python实现边缘提取

    1.  题目描述 安装opencv环境,实现边缘提取 2.  实现过程 1. 安装opencv+python环境 2. 打开图片 3. 将图片二值化 4. 提取边缘 5. 显示图片 3.  运行结果 ...

  9. Python--oop面向对象的学习1

    类和对象的成员分析 ·类和对象都可以存储成员,成员可以归类为所有,也可以归对象所有 ·类存储成员时使用的是与类关联的一个对象 ·独享存储成员时存储在当前对象中 ·对象访问一个成员,如果对象中没有该成员 ...

  10. DVWA-反射型XSS

    0x01 XSS介绍 XSS,全称Cross Site Scripting,即跨站脚本攻击,某种意义上也是一种注入攻击,是指攻击者在页面中注入恶意的脚本代码,当受害者访问该页面时,恶意代码会在其浏览器 ...