
Check if uppercase characters in a string are used correctly or not
Given a string S consisting of uppercase and lowercase letters, the task is to check if uppercase characters are used correctly in the given string or not. Correct usage of uppercase characters are as follows:
- All characters in the string are in uppercase. For example, “GEEKS”.
- None of the characters are in uppercase. For example, “geeks”.
- Only the first character is in uppercase. For example, “Geeks”.
Examples:
Input: S = “Geeks”
Output: Yes
Explanation: Only the first character of the string is in uppercase and all the remaining characters are in lowercase.Input: S = “GeeksForGeeks”
Output: No
Approach: Follow the steps below to solve the problem:
- Check if the first character of the string is in uppercase or not. If found to be true, iterate over the remaining characters.
- If all the remaining characters are in uppercase, print “Yes”. Otherwise, if any of the remaining characters are in uppercase, then print “NO”.
- If the first character is not in uppercase, then check if all the remaining characters are in lowercase or not. If found to be true, then print “YES”. Otherwise, print “NO”.
Below is the implementation of the above approach:
C++
|
No
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.
0 comments:
Post a Comment