密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
一组字符串。
如果符合要求输出:OK,否则输出NG
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000
OK NG NG OK
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args)throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String line=null; while((line=br.readLine())!=null){ check(line); } } private static boolean check(String str) { // 长度超过8位 if (str.length() < 8) { System.out.println("NG"); return false; } // 包括至少3种 int count = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') { count |= 1 << 0; } else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') { count |= 1 << 1; } else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') { count |= 1 << 2; } else { count |= 1 << 3; } } if (Integer.bitCount(count) < 3) { System.out.println("NG"); return false; } // 不能有长度大于2的包含公共元素的子串重复,子串长度至少3 for (int i = 0; i < str.length() - 3; i++) { String sub = str.substring(i, i + 3); if (str.indexOf(sub, i + 3) != -1) { System.out.println("NG"); return false; } } System.out.println("OK"); return true; } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { String text = in.nextLine(); System.out.println(text.matches(("^(?![a-zA-Z]+$)" + "(?![a-z\\d]+$)" + "(?![a-z[^A-Za-z0-9]]+$)" + "(?![\\d[^A-Za-z0-9]]+$)" + "(?![\\dA-Z]+$)" + "(?![[^A-Za-z0-9]A-Z]+$)" + "[A-Za-z\\d[^A-Za-z0-9]]{8,}$"))&&text.matches("^(?!.*(.{3,}).*\\1).*$") ? "OK" : "NG"); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String str = in.nextLine(); int res = 0; if (str.contains(" ") || str.length() <= 8 || !check(str)) { System.out.println("NG"); continue; } if(!crl("[a-z]",str)) res++; if(!crl("[A-Z]",str)) res++; if(!crl("[0-9]",str)) res++; if(!crl("[^a-zA-Z0-9]",str)) res++; if (res >= 3) { System.out.println("OK"); } else { System.out.println("NG"); } } } public static boolean crl(String str, String s) { return s.length() == s.replaceAll(str,"").length(); } public static boolean check(String str) { for (int i = 0; i < str.length(); i ++) { for (int j = i + 3; j < str.length() - 3; j++) { if (str.substring(i, i + 3).contains(str.substring(j, j + 3))) { return false; } } } return true; } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); while ((in.hasNextLine())){ String str=in.nextLine(); int flag=0; if(str.length()>8){ int arr[]=new int[4]; arr[0]=0;arr[1]=0;arr[2]=0;arr[3]=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)>='a'&&str.charAt(i)<='z'){ arr[0]=arr[0]+1; }else if(str.charAt(i)>='A'&&str.charAt(i)<='Z'){ arr[1]=arr[1]+1; }else if(str.charAt(i)>='0'&&str.charAt(i)<='9') { arr[2]=arr[2]+1; }else if(str.charAt(i)!=' '){ arr[3]=arr[3]+1; } } int count=0; for(int i=0;i<=3;i++){ if(arr[i]>0){ count++; } } if(count>=3){ for(int i=0;i<str.length()-5;i++){ for(int j=i+3;j<str.length()-2;j++){ if(str.charAt(i)==str.charAt(j)&& str.charAt(i+1)==str.charAt(j+1)&& str.charAt(i+2)==str.charAt(j+2)){ flag++; break; } } } }else{ flag++; } }else{ flag++; } if(flag==0){ System.out.println("OK"); }else { System.out.println("NG"); } } } }
import java.util.*; import java.util.regex.Pattern; //用find 匹配单个元素,用set去重看是否有重复子串 public class Main { public String isValid(String str){ if (str.length()<=8){return "NG"; } Pattern lowPattern = Pattern.compile("[a-z]"); Pattern upPattern = Pattern.compile("[A-Z]"); Pattern numPattern = Pattern.compile("\\d"); Pattern otherPattern = Pattern.compile("[^a-zA-z0-9\\t\\n]"); int cnt=0; if (lowPattern.matcher(str).find()){cnt+=1;} if (upPattern.matcher(str).find()){cnt+=1;} if (numPattern.matcher(str).find()){cnt+=1;} if (otherPattern.matcher(str).find()){cnt+=1;} if (cnt<3){return "NG";} HashSet<String> set = new HashSet<>(); for (int i = 0; i < str.length()-2; i++) { String newStr = str.substring(i, i + 3); set.add(newStr); } if (set.size()<str.length()-2){return "NG";} return "OK"; } public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String str=in.nextLine(); System.out.println(new Main().isValid(str)); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String a = in.nextLine(); if(a.length()<=8){ System.out.println("NG"); continue; } int count=0; if(a.matches(".*[a-z].*")){ count++; } if(a.matches(".*[A-Z].*")){ count++; } if(a.matches(".*[0-9].*")){ count++; } if(a.matches(".*[^a-zA-Z0-9].*")){ count++; } if(count<3){ System.out.println("NG"); continue; } boolean dul=false; for(int i=0;i<a.length()-4;i++){ String left=a.substring(i+3); String cur=a.substring(i,i+3); if(left.contains(cur)&&!cur.isEmpty()){ dul=true; break; } } if(dul){ System.out.println("NG"); }else{ System.out.println("OK"); } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String input = in.nextLine(); System.out.println(ifOK(input)); } } // public static String ifOK(String input) { String result = new String(); int charTypeNum = 0; //密码长度 if (input.length() < 9) { result = "NG"; return result; } //符号种类 for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= 'A' && input.charAt(i) <= 'Z') { charTypeNum++; break; } } for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= 'a' && input.charAt(i) <= 'z') { charTypeNum++; break; } } for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= '0' && input.charAt(i) <= '9') { charTypeNum++; break; } } for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= 'A' && input.charAt(i) <= 'Z') { continue; } if (input.charAt(i) >= 'a' && input.charAt(i) <= 'z') { continue; } if (input.charAt(i) >= '0' && input.charAt(i) <= '9') { continue; } if (input.charAt(i) == ' ' || input.charAt(i) == '\n') { continue; } charTypeNum++; break; } if (charTypeNum < 3) { result = "NG"; return result; } //子串 for (int i = 0; i < input.length() - 3; i++) { String tempStr = input.substring(i, i + 3); String tempStr2 = input.substring(i + 3, input.length()); if (tempStr2.contains(tempStr)) { result = "NG"; return result; } } result = "OK"; return result; } }
public class Test04 { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String pwd = in.nextLine(); System.out.println(isValid(pwd)?"OK":"NG"); } } public static boolean isValid(String pwd){ //长度小于8 if(pwd.length()<8){ return false; } int lowerCase = 0; int upperCase = 0; int digit = 0; int other = 0; for (int i = 0; i < pwd.length(); i++) { char a = pwd.charAt(i); if (Character.isLowerCase(a)) { lowerCase = 1; } else if (Character.isUpperCase(a)) { upperCase = 1; } else if (Character.isDigit(a)) { digit = 1; } else if (pwd.contains("\n") || pwd.contains(" ")) { } else { other = 1; } } //大小写,数字,其他符号至少三种(其他符号不包含空格和换行) if(lowerCase+upperCase+digit+other<3){ return false; } for (int i = 0; i < pwd.length()-2; i++) { //双指针找出长度大于2的子串 int j = i+3; String sub = pwd.substring(i, j); /*判断子串第一次出现和最后一次出现的位置 (这里ABABA也会为false) if(pwd.indexOf(sub)!=pwd.lastIndexOf(sub)){ return false; } */ if (pwd.substring(j).contains(sub)) { return false; } } return true; } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 判断密码是否符合要求 while (in.hasNext()) { String pass = in.nextLine(); if (checkPassword(pass)) { System.out.println("OK"); } else { System.out.println("NG"); } } } public static boolean checkPassword(String pass) { //判断长度是否超过8位,小于8,返回false if (pass.length() < 8){ return false; } // 定义正则表达式 String num = ".*\\d.*"; String upper = ".*[A-Z].*"; String lower = ".*[a-z].*"; String special = ".*[^a-zA-Z\\d].*"; int count = 0; //是否包含 数字 if (pass.matches(num)) { count++; } //是否包含 大写字母 if (pass.matches(upper)) { count++; } //是否包含 小写字母 if (pass.matches(lower)) { count++; } //是否包含 其它符号 if (pass.matches(special)) { count++; } // 判断密码是否符合正则表达式 以上四种至少三种 if (count >= 3) { // 判断是否有长度大于2的包含公共元素的子串重复 for (int i = 0; i < pass.length() - 3; i++) { String sub1 = pass.substring(i, i + 3); if (pass.substring(i + 3).contains(sub1)) { return false; } } return true; } return false; } }
思路: 简单粗暴
// 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNext()) { String data = input.nextLine(); if (isPassWordValid(data)) { System.out.println("OK"); } else { System.out.println("NG"); } } } private static boolean isPassWordValid(String data) { // 长度不超过8 if (data.length() <= 8) { return false; } // 复杂度 if (!checkPasswordComplexity(data)) { return false; } // 长度超过3的重复子串 for (int i = 0; i < data.length() - 2; i++) { String substring = data.substring(i, i + 3); int index = data.indexOf(substring); if (data.contains(substring) && index != i) { return false; } } return true; } private static boolean checkPasswordComplexity(String data) { // 大小写字母.数字.其它符号 int temp = 0; // 是否包含大写字母 if (data.matches(".*\\p{Upper}.*")) { temp++; } // 是否包含小写字母 if (data.matches(".*\\p{Lower}.*")) { temp++; } // 是否包含数字 if (data.matches(".*\\p{Digit}.*")) { temp++; } // 是否包含特殊字符 if (data.matches(".*\\p{Punct}.*")) { temp++; } return temp >= 3; } }
private static String checkEligiblePassword(String password) { if (null == password || password.length() < 9) { return "NG"; } if (password.contains(" ") || password.contains("\n")) { return "NG"; } int[] charType = {0, 0, 0, 0}; char[] passwordArr = password.toCharArray(); for (char str : passwordArr) { if (str >= 'A' && str <= 'Z') { charType[0] = 1; } else if (str >= 'a' && str <= 'z') { charType[1] = 1; } else if (str >= '0' && str <= '9') { charType[2] = 1; } else { charType[3] = 1; } } if (charType[0] + charType[1] + charType[2] + charType[3] < 3) { return "NG"; } for (int i = 3; i <= password.length() - 3; i++) { String prefix = password.substring(i - 3, i); String sufffix = password.substring(i); if (sufffix.contains(prefix)) { return "NG"; } } return "OK"; }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String line = in.nextLine(); if (line.length() <= 8) { System.out.println("NG"); continue; } int count = 0; boolean checkUp = true; boolean checkLow = true; boolean checkNum = true; boolean checkElse = true; char[] chars = line.toCharArray(); for (char c : chars) { if (c >= 'a' && c <= 'z') { if (checkLow) { checkLow = false; count++; } } else if (c >= 'A' && c <= 'Z') { if (checkUp) { checkUp = false; count++; } } else if (c >= '0' && c <= '9') { if (checkNum) { checkNum = false; count++; } } else { if (checkElse && c != ' ' && c != '\n') { checkElse = false; count++; } } } if (count < 3) { System.out.println("NG"); continue; } if (chongfu(line))System.out.println("NG"); else System.out.println("OK"); } } public static boolean chongfu(String line) { char[] chars = line.toCharArray(); // 双指针判断子串是否重复 for (int i = 0; i < chars.length; i++) { for (int j = i + 1; j < chars.length-2; j++) { int same = 0; int n = 0; while (n <= 2) { if (chars[i + n] == chars[j + n])same++; n++; } if (same == 3)return true; } } return false; } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String s = in.nextLine(); if (s.length() < 9) { System.out.println("NG"); continue; } char[] cr = s.toCharArray(); int check = 0; // 用一个int值的低4位01值来记录四种符号是否出现过,最后的值只需要不小于0111即可 for (char c : cr) { if (c >= 'A' && c <= 'Z') { check |= (1 << 0); } else if (c >= 'a' && c <= 'z') { check |= (1 << 1); } else if (c >= '0' && c <= '9') { check |= (1 << 2); } else { check |= (1 << 3); } } if (check < 7) { System.out.println("NG"); continue; } // 使用set来校验重复子串 HashSet<String> set = new HashSet<>(); int t = 0; boolean f = true; while (t < s.length()) { String ss = s.substring(t, Math.min(t + 3, s.length())); if (set.contains(ss)) { System.out.println("NG"); f = false; break; } set.add(ss); t++; } if (f) { System.out.println("OK"); } } } }