Minimum characters required to make a password strong

Given a string str denoting a password, the task is to count the minimum characters that need to be added to make the password strong.
A password is said to be strong if it satisfies the following criteria:

  • It contains at least 8 characters.
  • It contains at least one digit.
  • It contains at least one lower case alphabet.
  • It contains at least one upper case alphabet.
  • It contains at least one special character which includes !@#$%^&*()-+


Input: str = “Geeksforgeeks”
Output: 2
By adding one digit and one special character we can make the password strong.

Input: str = “Geeks1”
Output: 2
A special character along with one more character needs to be added to make the password strong.

Approach: This problem can be solved using Regular Expressions:

  • Create the regular expressions to check digit, special character, upper case and lower case alphabet.
  • Compile the regular expressions and find matches with the password string.
  • Increase the counter, say count, to add characters whenever any of the regular expression does not match.
  • After adding count characters to the password string, check if the length of the string is less than 8 or not. If so, add the difference to count and print.

Below is the implementation of the above approach.






// Java program to find minimum number
// of characters required to be added
// to make a password strong
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class GFG {
    // Function to count minimum
    // number of characters
    static int countCharacters(
        String password)
        int count = 0;
        // Create the patterns to search digit,
        // upper case alphabet, lower case
        // alphabet and special character
        Pattern digit = Pattern.compile("(\\d)");
        Pattern upper = Pattern.compile("([A-Z])");
        Pattern lower = Pattern.compile("([a-z])");
        Pattern spChar = Pattern.compile("(\\W)");
        // Search the above patterns in password.
        Matcher Digit = digit.matcher(password);
        Matcher Upper = upper.matcher(password);
        Matcher Lower = lower.matcher(password);
        Matcher Special = spChar.matcher(password);
        // If no digits are present
        if (!Digit.find()) {
        // If no upper case alphabet is present
        if (!Upper.find()) {
        // If no lower case alphabet is present
        if (!Lower.find()) {
        // If no special character is is present
        if (!Special.find()) {
        // Check if the string length after adding
        // the required characters is less than 8
        if ((count + password.length()) < 8) {
            // Add the number of
            // characters to be added
            count = count + 8
                    - (count + password.length());
        return count;
    // Driver Code
    public static void main(String args[])
        String password1 = "Geeksforgeeks";
        String password2 = "Geeks1";




Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.