Given string str of length N, the task is to check whether the given string contains uppercase alphabets, lowercase alphabets, special characters, and numeric values or not. If the string contains all of them, then print “Yes”. Otherwise, print “No”.
Examples:
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.
Naive Approach: The simplest approach is to iterate over the string and check if the given string contains uppercase, lowercase, numeric and special characters. Below are the steps:
- Traverse the string character by character from start to end.
- Check the ASCII value of each character for the following conditions:
- If the ASCII value lies in the range of [65, 90], then it is an uppercase letter.
- If the ASCII value lies in the range of [97, 122], then it is a lowercase letter.
- If the ASCII value lies in the range of [48, 57], then it is a number.
- If the ASCII value lies in the ranges [32, 47], [58, 64], [91, 96] or [123, 126], then it is a special character
- Print Yes if the string contains all the above. Otherwise, print No.
Time Complexity: O(N)
Auxiliary Space: O(1)
Regular Expression Approach: The idea is to the concept of a regular expression to solve this problem. Below are the steps:
- Create a regular expression to check if the given string contains uppercase, lowercase, special character, and numeric values as mentioned below:
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.
- Match the given string with the Regular Expression using Pattern.matcher().
- Print Yes if the string matches with the given regular expression. Otherwise, print No.
Below is the implementation of the above approach:
// C++ program to check if the string // contains uppercase, lowercase // special character & numeric value #include <iostream> #include <regex> 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" <<endl;
return ;
}
// Print Yes If the string matches
// with the Regex
if (regex_match(str, pattern))
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return ;
} // Driver Code int main()
{ string str = "#GeeksForGeeks123@" ;
isAllPresent(str);
return 0;
} // This code is contributed by yuvraj_chandra |
// 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 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 |
// C# program for the above approach using System;
using System.Text.RegularExpressions;
class GFG {
// Function that checks if a string
// contains uppercase, lowercase
// special character & numeric value
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
Regex p = new Regex(regex);
// If the string is empty
// then print No
if (str == null ) {
Console.Write( "No" );
return ;
}
// Find match between given string
// & regular expression
Match m = p.Match(str);
// Print Yes if string
// matches ReGex
if (m.Success)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
// Driver Code
public static void Main()
{
// Given string
string str = "#GeeksForGeeks123@" ;
// Function Call
isAllPresent(str);
}
} // This code is contributed by Samim Hossain Mondal |
<script> // JavaScript program to check if the string
// contains uppercase, lowercase
// special character & numeric value
function isAllPresent(str) {
// Regex to check if a string
// contains uppercase, lowercase
// special character & numeric value
var pattern = new RegExp(
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[-+_!@#$%^&*.,?]).+$"
);
// If the string is empty
// then print No
if (!str || str.length === 0) {
document.write( "No" + "<br>" );
return ;
}
// Print Yes If the string matches
// with the Regex
if (pattern.test(str)) {
document.write( "Yes" + "<br>" );
} else {
document.write( "No" + "<br>" );
}
return ;
}
// Driver Code
var str = "#GeeksForGeeks123@" ;
isAllPresent(str);
</script>
|
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)