给定长度为N 的字符串str ,任务是检查给定的字符串是否包含大写字母、小写字母、特殊字符和数值。如果字符串包含所有这些,则打印“Yes” 。否则,打印“否” 。
例子:
Input: str = “#GeeksForGeeks123@”
Output: Yes
Explanation:
The given string contains uppercase characters(‘G’, ‘F’), lowercase characters(‘e’, ‘k’, ‘s’, ‘o’, ‘r’), special characters( ‘#’, ‘@’), and numeric values(‘1’, ‘2’, ‘3’). Therefore, the output is Yes.
Input: str = “GeeksForGeeks”
Output: No
Explanation:
The given string contains only uppercase characters and lowercase characters. Therefore, the output is No.
朴素的方法:最简单的方法是遍历字符串并检查给定的字符串包含大写、小写、数字和特殊字符。以下是步骤:
- 通过字符遍历字符串的字符从开始到结束。
- 在以下条件下检查每个字符的 ASCII 值:
- 如果 ASCII 值在[65, 90]范围内,则它是大写字母。
- 如果 ASCII 值在[97, 122]的范围内,则它是小写字母。
- 如果 ASCII 值在[48, 57]的范围内,则它是一个数字。
- 如果 ASCII 值位于[32, 47] , [58, 64] , [91, 96]或[123, 126] 范围内,则它是特殊字符
- 如果字符串包含以上所有内容,则打印Yes 。否则,打印No 。
时间复杂度: O(N)
辅助空间: O(1)
正则表达式方法:这个想法就是用正则表达式的概念来解决这个问题。以下是步骤:
- 创建一个正则表达式来检查给定的字符串包含大写、小写、特殊字符和数值,如下所述:
regex = “^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)” + “(?=.*[-+_!@#$%^&*., ?]).+$”
where,
- ^ represents the starting of the string.
- (?=.*[a-z]) represent at least one lowercase character.
- (?=.*[A-Z]) represents at least one uppercase character.
- (?=.*\\d) represents at least one numeric value.
- (?=.*[-+_!@#$%^&*., ?]) represents at least one special character.
- . represents any character except line break.
- + represents one or more times.
- 使用 Pattern.matcher() 将给定的字符串与正则表达式匹配。
- 如果字符串与给定的正则表达式匹配,则打印Yes 。否则,打印No 。
下面是上述方法的实现:
C++
// C++ program to check if the string
// contains uppercase, lowercase
// special character & numeric value
#include
#include
using namespace std;
void isAllPresent(string str)
{
// Regex to check if a string
// contains uppercase, lowercase
// special character & numeric value
const regex pattern("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[-+_!@#$%^&*.,?]).+$");
// If the string is empty
// then print No
if (str.empty())
{
cout<<"No"<
Java
// Java program for the above approach
import java.util.regex.*;
class GFG {
// Function that checks if a string
// contains uppercase, lowercase
// special character & numeric value
public static void
isAllPresent(String str)
{
// ReGex to check if a string
// contains uppercase, lowercase
// special character & numeric value
String regex = "^(?=.*[a-z])(?=."
+ "*[A-Z])(?=.*\\d)"
+ "(?=.*[-+_!@#$%^&*., ?]).+$";
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the string is empty
// then print No
if (str == null) {
System.out.println("No");
return;
}
// Find match between given string
// & regular expression
Matcher m = p.matcher(str);
// Print Yes if string
// matches ReGex
if (m.matches())
System.out.println("Yes");
else
System.out.println("No");
}
// Driver Code
public static void main(String args[])
{
// Given string
String str = "#GeeksForGeeks123@";
// Function Call
isAllPresent(str);
}
}
Python3
# Python3 program for the
# above approach
import re
# Function that checks if a string
# contains uppercase, lowercase
# special character & numeric value
def isAllPresent(str):
# ReGex to check if a string
# contains uppercase, lowercase
# special character & numeric value
regex = ("^(?=.*[a-z])(?=." +
"*[A-Z])(?=.*\\d)" +
"(?=.*[-+_!@#$%^&*., ?]).+$")
# Compile the ReGex
p = re.compile(regex)
# If the string is empty
# return false
if (str == None):
print("No")
return
# Print Yes if string
# matches ReGex
if(re.search(p, str)):
print("Yes")
else:
print("No")
# Driver code
# Given string
str = "#GeeksForGeeks123@"
#Function Call
isAllPresent(str)
# This code is contributed by avanitrachhadiya2155
Javascript
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live