Friday, May 2, 2014

Password Check [Codeforces Coder-Strike 2014 - Qualification Round] Problem A

A. Password Check
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You have probably registered on Internet sites many times. And each time you should enter your invented password. Usually the registration form automatically checks the password's crypt resistance. If the user's password isn't complex enough, a message is displayed. Today your task is to implement such an automatic check.
Web-developers of the company Q assume that a password is complex enough, if it meets all of the following conditions:
  • the password length is at least 5 characters;
  • the password contains at least one large English letter;
  • the password contains at least one small English letter;
  • the password contains at least one digit.
You are given a password. Please implement the automatic check of its complexity for company Q.
Input
The first line contains a non-empty sequence of characters (at most 100 characters). Each character is either a large English letter, or a small English letter, or a digit, or one of characters: "!", "?", ".", ",", "_".
Output
If the password is complex enough, print message "Correct" (without the quotes), otherwise print message "Too weak" (without the quotes).
Sample test(s)
input
abacaba
output
Too weak
input
X12345
output
Too weak
input
CONTEST_is_STARTED!!11
output
Correct
→ Source
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PasswordCheck {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        
        if(s.length()<5){
            System.out.println("Too weak");
            System.exit(0);
        }
        int l=0,ss=0,d=0;
        
        Pattern p = Pattern.compile("[0-9]+");
        Matcher m = p.matcher(s);
        if(m.find()){
            d++;
        }
        p=Pattern.compile("[A-Z]+");
        m=p.matcher(s);
        if(m.find()){
            l++;
        }
        p=Pattern.compile("[a-z]+");
        m=p.matcher(s);
        if(m.find()){
            ss++;
        }
        
        if(l==0||ss==0||d==0){
            System.out.println("Too weak");
            System.exit(0);
        }
        System.out.println("Correct");
    }
}

No comments:

Post a Comment