给定表示密码的字符串str ,任务是计算需要添加以使密码强的最少字符数。
如果密码满足以下条件,则称其为强密码:
- 它至少包含 8 个字符。
- 它至少包含一位数字。
- 它至少包含一个小写字母。
- 它至少包含一个大写字母。
- 它至少包含一个特殊字符,其中包括!@#$%^&*()-+
例子:
Input: str = “Geeksforgeeks”
Output: 2
Explanation:
By adding one digit and one special character we can make the password strong.
Input: str = “Geeks1”
Output: 2
Explanation:
A special character along with one more character needs to be added to make the password strong.
方法:这个问题可以使用正则表达式解决:
- 创建正则表达式来检查数字、特殊字符、大写和小写字母。
- 编译正则表达式并找到与密码字符串匹配的内容。
- 增加计数器,例如count ,以在任何正则表达式不匹配时添加字符。
- 添加计数字符密码字符串后,检查字符串的长度小于8或没有。如果是,请添加差值以进行计数和打印。
下面是上述方法的实现。
Java
// 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()) {
count++;
}
// If no upper case alphabet is present
if (!Upper.find()) {
count++;
}
// If no lower case alphabet is present
if (!Lower.find()) {
count++;
}
// If no special character is is present
if (!Special.find()) {
count++;
}
// 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";
System.out.println(
countCharacters(password1));
String password2 = "Geeks1";
System.out.println(
countCharacters(password2));
}
}
Python3
# Python3 program to find
# minimum number of characters
# required to be added to make
# a password strong
import re
# Function to count minimum
# number of characters
def countCharacters(password):
count = 0
# Create the patterns to search digit,
# upper case alphabet, lower case
# alphabet and special character
digit = re.compile("(\\d)")
upper = re.compile("([A-Z])")
lower = re.compile("([a-z])")
spChar = re.compile("(\\W)")
# Search the above patterns
# in password.
# If no digits are present
if(not re.search(digit,
password)):
count += 1
# If no upper case alphabet
# is present
if(not re.search(upper,
password)):
count += 1
# If no lower case alphabet
# is present
if(not re.search(lower,
password)):
count += 1
# If no special character
# is is present
if(not re.search(spChar,
password)):
count += 1
# Check if the string length
# after adding the required
# characters is less than 8
if((count +
len(password)) < 8):
# Add the number of
# characters to be added
count = count + 8 - (count +
len(password))
return count
# Driver code
password1 = "Geeksforgeeks"
print(countCharacters(password1))
password2 = "Geeks1"
print(countCharacters(password2))
# This code is contributed by avanitrachhadiya2155
输出:
2
2
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live