Strong Password

Earlier this morning I took a look at the Strong Password challenge in HackerRank. I just want to exercise my skills processing text / strings with Java.

The idea is to check if a password entered by a user follows the requirements stipulated in the challenged for a strong password. If it does not, one needs to return the number (not types) of characters missing to make current password specified by the user strong.

My solution for the challenge follows:

   // Return the minimum number of characters to make the password strong
    static int minimumNumber(int n, String password) {
    	
    	// **** ****
    	final String 	numbers 			= "0123456789";
    	final String	lowerCase			= "abcdefghijklmnopqrstuvwxyz";
    	final String	upperCase 			= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	final String	specialCharacters 	= "!@#$%^&*()-+";
    	
    	// **** number of possible missing characters ****
    	int m 		= 4;
    	int mask	= 0x0f;
    	
    	// **** traverse the password ****
    	for (int i = 0; (i < password.length()) && (m > 0); i++) {
    		
    		// **** current character ****
    		char c = password.charAt(i);

    		// **** check in numbers (mask = 0x01) ****
        	if (	(((mask & 0x01) == 1) ? true : false) && 
        			(numbers.indexOf(c) != -1)) {
        		m--;
        		mask &= 0x0e;
        	}
        	
        	// **** check lower case letters (mask = 0x02) ****    		
        	if (	(((mask & 0x02) == 2) ? true : false) && 
        			(lowerCase.indexOf(c) != -1)) {
        		m--;
        		mask &= 0x0d;
        	}
        		
           	// **** check upper case letters (mask = 0x04) ****
        	if (	(((mask & 0x04) == 4) ? true : false) && 
        			(upperCase.indexOf(c) != -1)) {
        		m--;
        		mask &= 0x0b;
        	}

        	// **** check special characters (mask = 0x08) ****
        	if (	(((mask & 0x08) == 8) ? true : false) && 
        			(specialCharacters.indexOf(c) != -1)) {
        		m--;
        		mask &= 0x07;
        	}
    	}
    	
    	// **** check the password length ****
    	if ((n < 6) && 
    		((n + m) < 6))
    			m = 6 - n;

    	// **** missing count ****
    	return m;
    }

Simple and seems to work; at least it was accepted.

The code traverses the password. It stops traversing it if it matches the requirements (number of characters and types). I used a mask to check when a type of characters is encountered so we can skip the check on future loops.

We also check the number of characters at the end. The reason for this is to check for the case when a user has entered a long enough password but it may only contain one type of characters (e.g., “1111111111”). The user may also enter a password with all the required types of characters, but it may be short (e.g., “0aA!”).

If you are interested in getting the entire code for the solution you can find it in my GitHub repository.

Keep on learning, experimenting and having fun.

If you have comments or questions regarding this or any other post in this blog; please leave me a note below.

John

Please follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.