Count Strings that Do Not Contain Any Alphabet’s Both Uppercase and Lowercase

Ishita Chawla
Last Updated: May 13, 2022

Introduction

Arrays and strings form the backbone of competitive programming and data structures and algorithms.  So, it is crucial to have a tight grip over these basic topics before implementing and solving complex and more challenging problems. 
 

In this blog, we will discuss one such fundamental and easy problem: to count strings that do not contain any alphabet’s both uppercase and lowercase. 

Problem Statement

You have been provided with an array ofstrings. Your task is to determine the strings' count that contains only uppercase characters or only lowercase characters, but not both. 

Let us look at a few examples before moving on to its solution

Example

  • N = 5

stringArray = {INDIA, AUSTRAlia, Norway, LONDON, USA}

The first, fourth, and fifth strings contain all the alphabets in the uppercase. So strings satisfy our conditions. Hence the answer is 3
 

  • N = 4

stringArray = {dubai, aregeNTina, TAIWAN, IraQ}

The first string contains all the lowercase alphabets, while the third one has all the alphabets in the uppercase. So 2 strings satisfy our conditions. Hence the answer is 2. 

Algorithm

  • Consider a variable COUNT, initialized to 0, that will keep a count of the strings that satisfy the given condition.
  • Traverse the vector of strings and, at each iteration, initialize 2 variables, LOWER_CASE and UPPER_CASE, to 0. These variables will keep a count of the number of lowercase and uppercase characters in the string.
  • Traverse each of thestrings and increment LOWER_CASE and UPPER_CASE accordingly. 
  • If either one of LOWER_CASE or UPPER_CASE  is 0 and the other one is not, increment COUNT, as the conditions are satisfied. 
  • This COUNT will provide us with the final answer, so return it. 

Implementation

Program

// C++ program to count strings that do not contain any alphabet's both uppercase and lowercase.
#include <iostream>
#include <vector>
using namespace std;

// Function to count strings that do not contain any alphabet's both uppercase and lowercase.
int countStrings(vector<string> &stringArray)
{
    // Variable to store the answer.
    int count = 0;

    // Loop to access the individual strings of the vector of strings.
    for (int i = 0; i < stringArray.size(); i++)
    {
        int lowerCase = 0, upperCase = 0;

        // Traversing the strings and calculating the count of lowercase and uppercase alphabets.
        for (int j = 0; j < stringArray[i].size(); j++)
        {
            // Using ASCII values, calculating the count of lowercase alphabets.
            if (stringArray[i][j] >= 'a' && stringArray[i][j] <= 'z')
                lowerCase++;

            // Using ASCII values, calculating the count of uppercase alphabets.
            if (stringArray[i][j] >= 'A' && stringArray[i][j] <= 'Z')
                upperCase++;
        }

        // Increasing the count if the conditions are satisfied.
        if ((upperCase == 0 && lowerCase != 0) || (lowerCase == 0 && upperCase != 0))
            count++;
    }

    // Returning the answer.
    return count;
}

int main()
{
    int n;
    string k;

    vector<string> stringArray;

    // Taking user input.
    cout << "Enter the size of the array: ";
    cin >> n;
    cout << "Enter the strings into the array of strings:\n";
    for (int i = 0; i < n; i++)
    {
        cin >> k;
        stringArray.push_back(k);
    }

    // Calling the function and printing the answer.
    cout << "The count of strings that do not contain any alphabet’s both uppercase and lowercase is " << countStrings(stringArray);
    return 0;
}

Input

Enter the size of the array: 5
Enter the strings into the array of strings:
INDIA
AUSTRAlia
Norway
LONDON
USA

Output

The count of strings that do not contain any alphabet’s both uppercase and lowercase is 3

Time Complexity

The time complexity is O(N * M), where N is the size of the vector of strings and M is the length of the longest string.

We are traversing all the N strings from the vector and traversing the individual strings completely. Thus, the time complexity is O(N * M).

Space Complexity

The space complexity is O(1)

We are not using any extra space to store the array of strings, so the implementation uses constant space.

Key Takeaways

So, this blog discussed the problem to count strings that do not contain any alphabet’s both uppercase and lowercase. To discover more, go to CodeStudio right now to practice problems and ace your interviews like a Ninja!

 

In today's competitive environment, memorizing a handful of questions isn't enough. So, take our mock tests to evaluate where you stand among your peers and what areas you need to work on.

 

In case of any suggestions, please leave them in the comments box.

Was this article helpful ?
0 upvotes