Open In App

Check string for all uppercase-lowercase or first uppercase and rest lowercase characters

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

C++
#include <bits/stdc++.h>
using namespace std;

// Function to check whether ch is in uppercase
bool isUpperCase(char ch) { return ch >= 'A' && ch <= 'Z'; }

// function to check whether ch is in lowercase
bool isLowerCase(char ch) { return ch >= 'a' && ch <= 'z'; }

// function to check if S has only uppercase or only
// lowercase or only the first character is in uppercase and
// all the remaining characters are in lowercase
string solve(string S)
{
    // Variable to store the length of string S
    int len = S.length();

    // Variable to store the count of lowercase and
    // uppercase letters
    int countLower = 0, countUpper = 0;

    // Count the number of lowercase and uppercase letters
    for (int i = 0; i < len; i++) {
        if (isUpperCase(S[i]))
            countUpper += 1;
        else if (isLowerCase(S[i]))
            countLower += 1;
    }

    // If string S has only uppercase or only lowercase
    // letters
    if (countLower == len || countUpper == len)
        return "YES";

    // If string S has the first character as uppercase and
    // all the remaining characters as lowercase
    if (countUpper == 1 && countLower == len - 1
        && isUpperCase(S[0]))
        return "YES";

    // If no condition satisfies, return "NO"
    return "NO";
}

int main()
{
    // Sample Input
    cout << solve("Geeks") << endl;
    cout << solve("geeksforgeeks") << endl;
    cout << solve("GFG") << endl;
    cout << solve("geekS") << endl;
    return 0;
}
Java
public class CaseCheck {

    // Function to check whether ch is in uppercase
    static boolean isUpperCase(char ch) {
        return ch >= 'A' && ch <= 'Z';
    }

    // Function to check whether ch is in lowercase
    static boolean isLowerCase(char ch) {
        return ch >= 'a' && ch <= 'z';
    }

    // Function to check if S has only uppercase or only
    // lowercase or only the first character is in uppercase and
    // all the remaining characters are in lowercase
    static String solve(String S) {
        // Variable to store the length of string S
        int len = S.length();

        // Variable to store the count of lowercase and
        // uppercase letters
        int countLower = 0, countUpper = 0;

        // Count the number of lowercase and uppercase letters
        for (int i = 0; i < len; i++) {
            if (isUpperCase(S.charAt(i)))
                countUpper += 1;
            else if (isLowerCase(S.charAt(i)))
                countLower += 1;
        }

        // If string S has only uppercase or only lowercase
        // letters
        if (countLower == len || countUpper == len)
            return "YES";

        // If string S has the first character as uppercase and
        // all the remaining characters as lowercase
        if (countUpper == 1 && countLower == len - 1
                && isUpperCase(S.charAt(0)))
            return "YES";

        // If no condition satisfies, return "NO"
        return "NO";
    }

    public static void main(String[] args) {
        // Sample Input
        System.out.println(solve("Geeks"));
        System.out.println(solve("geeksforgeeks"));
        System.out.println(solve("GFG"));
        System.out.println(solve("geekS"));
    }
}

// This code is contributed by shivamgupta0987654321
JavaScript
// Function to check whether ch is in uppercase
function isUpperCase(ch) {
    return ch >= 'A' && ch <= 'Z';
}

// Function to check whether ch is in lowercase
function isLowerCase(ch) {
    return ch >= 'a' && ch <= 'z';
}

// Function to check if S has only uppercase or only lowercase or only the first character is in uppercase and all the remaining characters are in lowercase
function solve(S) {
    // Variable to store the length of string S
    const len = S.length;

    // Variable to store the count of lowercase and uppercase letters
    let countLower = 0, countUpper = 0;

    // Count the number of lowercase and uppercase letters
    for (let i = 0; i < len; i++) {
        if (isUpperCase(S[i]))
            countUpper += 1;
        else if (isLowerCase(S[i]))
            countLower += 1;
    }

    // If string S has only uppercase or only lowercase letters
    if (countLower === len || countUpper === len)
        return "YES";

    // If string S has the first character as uppercase and all the remaining characters as lowercase
    if (countUpper === 1 && countLower === len - 1 && isUpperCase(S[0]))
        return "YES";

    // If no condition satisfies, return "NO"
    return "NO";
}

// Sample Input
console.log(solve("Geeks"));
console.log(solve("geeksforgeeks"));
console.log(solve("GFG"));
console.log(solve("geekS"));
Python3
# Function to check whether ch is in uppercase
def is_upper_case(ch):
    return 'A' <= ch <= 'Z'

# Function to check whether ch is in lowercase
def is_lower_case(ch):
    return 'a' <= ch <= 'z'

# Function to check if S has only uppercase or only
# lowercase or only the first character is in uppercase and
# all the remaining characters are in lowercase
def solve(S):
    # Variable to store the length of string S
    len_S = len(S)

    # Variables to store the count of lowercase and
    # uppercase letters
    count_lower = 0
    count_upper = 0

    # Count the number of lowercase and uppercase letters
    for i in range(len_S):
        if is_upper_case(S[i]):
            count_upper += 1
        elif is_lower_case(S[i]):
            count_lower += 1

    # If string S has only uppercase or only lowercase
    # letters
    if count_lower == len_S or count_upper == len_S:
        return "YES"

    # If string S has the first character as uppercase and
    # all the remaining characters as lowercase
    if count_upper == 1 and count_lower == len_S - 1 and is_upper_case(S[0]):
        return "YES"

    # If no condition satisfies, return "NO"
    return "NO"

# Sample Input
print(solve("Geeks"))
print(solve("geeksforgeeks"))
print(solve("GFG"))
print(solve("geekS"))

Output
YES
YES
YES
NO



Time Complexity: O(N), where N is the length of input string S.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads