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”.
Input: str = “#GeeksForGeeks123@”
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”
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 character. 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)” + “(?=.*[-+_!@#$%^&*., ?]).+$”
- ^ 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:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Remove uppercase, lowercase, special, numeric, and non-numeric characters from a String
- Count Uppercase, Lowercase, special character and numeric values
- Check if lowercase and uppercase characters are in same order
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
- Program to display all alphabets from A to Z in uppercase and lowercase both
- Maximum distinct lowercase alphabets between two uppercase
- Sub-string that contains all lowercase alphabets after performing the given operation
- Split numeric, alphabetic and special symbols from a String
- Minimum number of operations to move all uppercase characters before all lower case characters
- Check if a string consists only of special characters
- Program to count vowels, consonant, digits and special characters in string.
- Check if a string contains only alphabets in Java using ASCII values
- Reverse a string without affecting special characters
- Calculate Sum of ratio of special characters to length of substrings of the given string
- Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels
- Python program to check if a string contains all unique characters
- Check if a String contains any index with more than K active characters
- String matching where one string contains wildcard characters
- Longest sub-array with equal number of alphabets and numeric characters
- First uppercase letter in a string (Iterative and Recursive)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : avanitrachhadiya2155