![]() |
VOOZH | about |
Given a string S, the task is to check if all the characters in S are in lowercase, or all the characters are in uppercase or only the first character is in uppercase, and the remaining characters are in lowercase. Print "YES" if any of the condition gets satisfied else print "NO".
Examples:
Input: S = "Geeks"
Output: YES
Explanation: Only the first character of S is in uppercase and all the remaining characters are in lowercase.Input: S = "geeksforgeeks"
Output: YES
Explanation: All the characters in S are in lowercase.Input: S = "GFG"
Output: YES
Explanation: All the characters in S are in uppercase.Input: S = "geekS"
Output: NO
Explanation: The string S does not have all uppercase, lowercase or only the first character as uppercase, therefore print "NO".
Approach: To solve the problem, follow the below idea:
The problem can be solved by counting all the uppercase and lowercase characters in the string S. If the count of uppercase characters is equal to the length of S or the count of lowercase characters is equal to length of S, then print "YES". Otherwise, if the count of uppercase is 1, count of lowercase characters is (length of S - 1) and the first character of S is in uppercase, then print "YES". Otherwise print "NO".
Below is the implementation of the above approach:
YES YES YES NO
Time Complexity: O(N), where N is the length of input string S.
Auxiliary Space: O(1)