首页 > 试题广场 >

密码验证合格程序

[编程题]密码验证合格程序
  • 热度指数:404078 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)

数据范围:输入的字符串长度满足

输入描述:

一组字符串。



输出描述:

如果符合要求输出:OK,否则输出NG

示例1

输入

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

输出

OK
NG
NG
OK
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            check(sc.nextLine());
        }
    }

    private static void check(String password) {
        if (password.length() <= 8) {
            System.out.println("NG");
            return;
        }
        int type = 0;
        boolean low = false;
        boolean up = false;
        boolean dig = false;
        boolean ch = false;
        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);
            if (c >= 'a' && c <= 'z') {
                low = true;
            } else if (c >= 'A' && c <= 'Z') {
                up = true;
            } else if (c >= '0' && c <= '9') {
                dig = true;
            } else {
                ch = true;
            }
        }
        if (low) {
            type++;
        }
        if (up) {
            type++;
        }
        if (dig) {
            type++;
        }
        if (ch) {
            type++;
        }
        if (type < 3) {
            System.out.println("NG");
            return;
        }
        for (int i = 0; i < password.length() - 3; i++) {
            String str = password.substring(i, i + 3);
            if (password.indexOf(str, i + 3) != -1) {
                System.out.println("NG");
                return;
            }
        }
        System.out.println("OK");
        return;
    }
}
编辑于 2024-04-16 22:29:03 回复(0)
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;
    }
}

编辑于 2024-04-11 13:30:28 回复(0)
编辑于 2024-03-28 13:05:31 回复(0)
正则表达式求解
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()){
            int j = 0;
            String str = in.nextLine();
            if(str.length() < 8){           //长度不符合要求
                System.out.println("NG");
                continue;
            }
            if(str.matches(".*[ ].*")){     //含有空格
                System.out.println("NG");
                continue;
            }
            if(str.matches(".*[A-Z].*")){   //含有大写字母
                j++;
            }
            if(str.matches(".*[a-z].*")){   //含有小写字母
                j++;
            }
            if(str.matches(".*[^A-Za-z0-9].*")){    //含有其他字符
                j++;
            }
            if(str.matches(".*[0-9].*")){   //含有数字
                j++;
            }
            if(j>=3 && !judgeString(str)){  //j>=3且不含有重复子串
                System.out.println("OK");
            }else{      //j<=3或者含有重复子串
                System.out.println("NG");
            }
        }
       
    }

     public static boolean judgeString(String str){
        for(int i = 1; i < str.length()-2; i++){
            if(str.substring(i).contains(str.substring(i-1,i+2))){
                return true;
            }
        }
        return false;
     }
}
编辑于 2024-03-11 00:55:20 回复(0)
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();
            if (str.length() < 8 || check(str)) {
                System.out.println("NG");
                continue;
            }
            int daxieCount = 0;
            int xiaoxieCount = 0;
            int zimuCount = 0;
            int teshuCount = 0;
            for(int i = 0;i<str.length();i++){
                char c = str.charAt(i);
                if (Character.isUpperCase(c)) {
                    daxieCount = 1;
                } else if (Character.isLowerCase(c)) {
                    xiaoxieCount= 1;
                } else if (Character.isDigit(c)) {
                    zimuCount= 1;
                } else {
                    teshuCount= 1;
                }
            }
            if(daxieCount+xiaoxieCount+zimuCount+teshuCount >= 3){
                System.out.println("OK");
            }else{
                System.out.println("NG");
            }
        }
    }

    public static boolean check(String str) {
        for (int i = 0; i < str.length()/2+1; i++) {
            String substring = str.substring(i, i + 3);
            if(str.substring(i+3).contains(substring)){
                return true;
            }
        }
        return false;
    }
}
发表于 2024-02-02 13:04:28 回复(0)
    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");
        }
    }

发表于 2023-11-03 09:30:43 回复(1)
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;
    }
}

发表于 2023-10-16 23:20:53 回复(1)
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");
            }
        }
    }
}

发表于 2023-09-15 19:25:43 回复(0)
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));
        }
    }

}

发表于 2023-09-15 00:34:19 回复(0)
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");
            }
           
        }
    }
}

发表于 2023-08-07 22:31:46 回复(0)
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;
    }
}

发表于 2023-07-03 09:08:18 回复(0)
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;
    }
}

发表于 2023-06-03 17:46:18 回复(0)
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;
    }
}

发表于 2023-05-23 10:19:34 回复(0)

思路: 简单粗暴

// 注意类名必须为 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;
    }
}
发表于 2023-05-05 23:48:35 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        LOOP:
        while (in.hasNext()) {
            String str = in.nextLine();
            //长度超过8位
            if (str.length() <= 8) {
                System.out.println("NG");
                continue LOOP;
            }
            int category = 0;
            boolean[] flags = new boolean[4];
            Set<String> set = new HashSet<String>();
            for (int i = 0; i < str.length(); i++) {
                //不能有长度大于2的包含公共元素的子串重复
                if (i <= str.length() - 3) {
                    if (!set.add(str.substring(i, i + 3))) {
                        System.out.println("NG");
                        continue LOOP;
                    }
                }
                //包括大小写字母.数字.其它符号,以上四种至少三种
                char c = str.charAt(i);
                if (Character.isDigit(c) && !flags[0]) {
                    flags[0] = true;
                    category++;
                } else if (Character.isLowerCase(c) && !flags[1]) {
                    flags[1] = true;
                    category++;
                } else if (Character.isUpperCase(c) && !flags[2]) {
                    flags[2] = true;
                    category++;
                } else if (Character.isSpace(c)) {
                    System.out.println("NG");
                    continue LOOP;
                } else if (!Character.isLetterOrDigit(c) && !flags[3]) {
                    flags[3] = true;
                    category++;
                }
            }
            if (category >= 3)
                System.out.println("OK");
            else
                System.out.println("NG");
        }
    }
}
发表于 2023-04-26 11:28:49 回复(0)
Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.next();
            String ret = "";
            if(a==null ||a.trim().length()<=8){
                ret = "NG";
            }else{
                char[] chars = a.toCharArray();
                int num1 = 0;
                int num2 = 0;
                int num3 = 0;
                int num4 = 0;
                boolean flag = true;
                for(int i=0;i<chars.length;i++){
                    char c = chars[i];
                    if(c>=48&&c<=57){
                        num1 = 1;
                    }else if(c>=65&&c<=90){
                        num2 = 1;
                    }else if(c>=97&&c<=122){
                        num3 = 1;
                    }else if(c<48 || c>57&&c<65 || c>90&&c<97){
                        num4 = 1;
                    }
                }
                if((num1+num2+num3+num4)<3){
                    flag = false;
                }
                Map<String,Integer> map = new HashMap<String,Integer>();
                for(int i=0;i<a.length();i++){
                     for(int j=a.length()-1;j>=i+3;j--){
                        String key1 = a.substring(i,j);
                        if(map.containsKey(key1)){
                            map.put(key1,map.get(key1)+1);
                        }else{
                            map.put(key1,1);
                        }
                    }
                }
                for(Map.Entry<String,Integer> entry:map.entrySet()){
                    if(entry.getValue()>1){
                        flag = false;
                        break;
                    }
                }

                if(flag){
                     ret = "OK";
                }else{
                     ret = "NG";
                }
               
            }
            System.out.println(ret);
        }
发表于 2023-04-15 21:32:39 回复(0)
    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";
    }


发表于 2023-04-14 17:50:52 回复(0)
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;
    }

}

发表于 2023-04-11 18:09:19 回复(0)
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");
            }
        }
    }
}

发表于 2023-04-07 19:36:38 回复(0)

问题信息

难度:
143条回答 92701浏览

热门推荐

通过挑战的用户

查看代码