Open In App

Categorize Password as Strong or Weak using Regex in Python

Last Updated : 15 Jul, 2025
Comments
Improve
Suggest changes
1 Like
Like
Report

Given a password, we have to categorize it as a strong or weak one. There are some checks that need to be met to be a strong password. For a weak password, we need to return the reason for it to be weak. Conditions to be fulfilled are:

  • Minimum 9 characters and maximum 20 characters.
  • Cannot be a newline or a space
  • There should not be three or more repeating characters in a row.
  • The same string pattern(minimum of two character length) should not be repeating.

Note: For checking the basic validations of a password, click here. Examples:

Input1 : Qggf!@ghf3
Output1 : Strong Password!

Input2 : aaabnil1gu
Output2 : Weak Password: Same character repeats three 
or more times in a row

Input3 : Geeksforgeeks
Output3 : Weak Password: Same character repeats three 
or more times in a row

Input4 : Aasd!feasnm
Output4 : Weak password: Same string pattern repetition

Input5 : 772*hdf77
Output5 : Weak password: Same string pattern repetition

Input6 : " "
Output6 : Password cannot be a newline or space!

Below is the implementation. 


Output
Strong Password!
Weak password: Same string pattern repetition
Weak Password: Same character repeats three or more times in a row
Weak password: Same string pattern repetition
Strong Password!
Password cannot be a newline or space!

Time Complexity: O(N)  N is refers to the length of the input password string,
Auxiliary Space: O(1) 


Practice Tags :

Similar Reads