题解 | #字符串分隔#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.*;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String input = scanner.next();
if(input.length() <= 8 || !getMatch(input) || containsRepeat(input,0, 3)) {
System.out.println("NG");
} else {
System.out.println("OK");
}
}
}
// 是否长度大于2的子串,即只要包含长度大于3的子串则返回true
private static boolean containsRepeat(String input, int start, int end) {
if(end > input.length()) {
return false;
}
if (input.substring(end).contains(input.substring(start, end))){
return true;
}
return containsRepeat(input, start +1, end+1);
}
private static boolean getMatch(String input) {
int count = 0;
Pattern pattern = Pattern.compile("[A-Z]");
if(pattern.matcher(input).find()){
count++;
}
Pattern pattern2 = Pattern.compile("[a-z]");
if(pattern2.matcher(input).find()){
count++;
}
Pattern pattern3 = Pattern.compile("[0-9]");
if(pattern3.matcher(input).find()){
count++;
}
Pattern pattern4 = Pattern.compile("[^a-zA-Z0-9]");
if(pattern4.matcher(input).find()){
count++;
}
return count >= 3 ? true : false;
}
}
java全栈日日学 文章被收录于专栏
java全栈每日必学,不要高估自己一年能做的事,不要低估自己十年能做的事