Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count Strings that does not contain any alphabet’s both uppercase and lowercase

  • Last Updated : 19 Dec, 2021

Given an array of strings arr[], containing N strings, the task is to count the number of strings that do not contain both the uppercase and the lowercase character of an alphabet.

Example:

Input: arr[]={ “abcdA”, “abcd”, “abcdB”, “abc” }
Output: 2
Explanation: The first string contains both the uppercase and the lowercase character for alphabet ‘a’. Similarly 3rd string also contains the uppercase and the lowercase character for alphabet ‘b’. Hence the count of valid strings is 3.

Input: arr[]={ “xyz”, “abc”, “nmo” }
Output: 3

Approach: The given problem can be solved using a greedy approach by iterating through all the given strings and for each alphabet check if the given string contains both its uppercase and the lowercase counterparts. Follow the below steps to solve this problem:

  1. Create a variable count to store the required count. Initialize it with 0.
  2. Now, traverse on each string in array arr[] and for each string store the frequency of its lowercase characters in an unordered map, M.
  3. Now traverse on that string, and for each uppercase letter check if the frequency of its lowercase counterpart is greater than zero. if it is, then increment the value of count by 1.
  4. Return count as the final answer.

Below is the implementation of the above approach:

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find count of strings that
// do not contain the uppercase and
// lowercase character of same alphabet
int countStrings(vector<string>& arr)
{
    // Variable to store the answer
    int count = 0;
 
    // Loop to iterate through
    // the array arr[]
    for (auto x : arr) {
        bool isAllowed = 1;
 
        // Vector to store the frequency
        // of lowercase characters
        unordered_map<char, int> M;
 
        // Iterator through the
        // current string
        for (auto y : x) {
            if (y - 'a' >= 0 and y - 'a' < 26) {
                M[y]++;
            }
        }
 
        for (auto y : x) {
 
            // Check if any uppercase letter
            // have its lowercase version
            if (y - 'A' >= 0 and y - 'A' < 26
                and M[tolower(y)] > 0) {
                isAllowed = 0;
                break;
            }
        }
 
        // If current string is not a valid
        // string, increment the count
        if (isAllowed) {
            count += 1;
        }
    }
 
    // Return Answer
    return count;
}
 
// Driver Code
int main()
{
    vector<string> arr
        = { "abcdA", "abcd", "abcdB", "abc" };
    cout << countStrings(arr);
}

Java




// Java code for the above approach
import java.util.*;
 
class GFG{
 
// Function to find count of Strings that
// do not contain the uppercase and
// lowercase character of same alphabet
static int countStrings(String[]arr)
{
   
    // Variable to store the answer
    int count = 0;
 
    // Loop to iterate through
    // the array arr[]
    for (String x : arr) {
        boolean isAllowed = true;
 
        // Vector to store the frequency
        // of lowercase characters
        HashMap<Character,Integer> M = new HashMap<Character,Integer>();
 
        // Iterator through the
        // current String
        for (char y : x.toCharArray()) {
            if (y - 'a' >= 0 && y - 'a' < 26) {
                 if(M.containsKey(y)){
                        M.put(y, M.get(y)+1);
                    }
                    else{
                        M.put(y, 1);
                    }
            }
        }
 
        for (char y : x.toCharArray()) {
 
            // Check if any uppercase letter
            // have its lowercase version
            if (y - 'A' >= 0 && y - 'A' < 26 && M.containsKey(Character.toLowerCase(y))
                && M.get(Character.toLowerCase(y)) > 0) {
                isAllowed = false;
                break;
            }
        }
 
        // If current String is not a valid
        // String, increment the count
        if (isAllowed) {
            count += 1;
        }
    }
 
    // Return Answer
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    String []arr
        = { "abcdA", "abcd", "abcdB", "abc" };
    System.out.print(countStrings(arr));
}
}
// This code is contributed by 29AjayKumar

Python3




# Python code for the above approach
 
# Function to find count of strings that
# do not contain the uppercase and
# lowercase character of same alphabet
def countStrings(arr):
    # Variable to store the answer
    count = 0
 
    # Loop to iterate through
    # the array arr[]
    for x in arr:
        isAllowed = 1
 
        # Vector to store the frequency
        # of lowercase characters
        M = {}
 
        # Iterator through the
        # current string
        for y in x:
            if (ord(y) - ord('a') >= 0 and ord(y) - ord('a') < 26):
                if(y in M):
                    M[y] += 1
                else:
                    M[y] = 1
 
        for y in x:
 
            # Check if any uppercase letter
            # have its lowercase version
            if (ord(y) - ord('A') >= 0 and ord(y) - ord('A') < 26 and M[y.lower()] > 0):
                isAllowed = 0
                break
 
        # If current string is not a valid
        # string, increment the count
        if (isAllowed):
            count += 1
 
    # Return Answer
    return count
 
 
# Driver Code
 
arr = ["abcdA", "abcd", "abcdB", "abc"]
print(countStrings(arr))
 
# This code is contributed by gfgking.

C#




// C# code for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG
{
   
// Function to find count of strings that
// do not contain the uppercase and
// lowercase character of same alphabet
static int countStrings(ArrayList arr)
{
   
    // Variable to store the answer
    int count = 0;
 
    // Loop to iterate through
    // the array arr[]
    foreach (string x in arr) {
        bool isAllowed = true;
 
        // To store the frequency
        // of lowercase characters
        Dictionary<char, int> M =
          new Dictionary<char, int>();
 
        // Iterator through the
        // current string
        foreach (char y in x) {
            if (y - 'a' >= 0 && y - 'a' < 26) {
              if (M.ContainsKey(y))
                {
                    M[y] = M[y] + 1;
                }
                else
                {
                    M.Add(y, 1);
                }
            }
        }
 
        foreach (char y in x) {
 
            // Check if any uppercase letter
            // have its lowercase version
            if (y - 'A' >= 0  && y - 'A' < 26
                && M[Char.ToLower(y)] > 0) {
                isAllowed = false;
                break;
            }
        }
 
        // If current string is not a valid
        // string, increment the count
        if (isAllowed) {
            count += 1;
        }
    }
 
    // Return Answer
    return count;
}
 
// Driver Code
public static void Main()
{
    ArrayList arr = new ArrayList();
         
    arr.Add("abcdA");
    arr.Add("abcd");
    arr.Add("abcdB");
    arr.Add("abc");
     
    Console.Write(countStrings(arr));
}
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript




<script>
 
     // JavaScript Program to implement
     // the above approach
 
     // Function to find count of strings that
     // do not contain the uppercase and
     // lowercase character of same alphabet
     function countStrings(arr)
     {
      
         // Variable to store the answer
         let count = 0;
 
         // Loop to iterate through
         // the array arr[]
         for (let x of arr)
         {
             let isAllowed = 1;
 
             // Vector to store the frequency
             // of lowercase characters
             let M = new Map();
 
             // Iterator through the
             // current string
             for (let i = 0; i < x.length; i++) {
                 y = x[i];
                 if (y.charCodeAt(0) - 'a'.charCodeAt(0) >= 0 && y.charCodeAt(0) - 'a'.charCodeAt(0) < 26) {
                     if (M.has(y)) {
                         M.set(y, M.get(y) + 1);
                     }
                     else {
                         M.set(y, 1);
                     }
                 }
             }
 
             for (i = 0; i < x.length; i++)
             {
                 y = x[i];
                  
                 // Check if any uppercase letter
                 // have its lowercase version
                 if (y.charCodeAt(0) - 'A'.charCodeAt(0) >= 0 && y.charCodeAt(0) - 'A'.charCodeAt(0) < 26
                     && M.get(y.toLowerCase()) > 0) {
                     isAllowed = 0;
                     break;
                 }
             }
 
             // If current string is not a valid
             // string, increment the count
             if (isAllowed) {
                 count += 1;
             }
         }
 
         // Return Answer
         return count;
     }
 
     // Driver Code
     let arr
         = ["abcdA", "abcd", "abcdB", "abc"];
     document.write(countStrings(arr));
 
 // This code is contributed by Potta Lokesh
 </script>
Output
2

Time Complexity: O(N * M), where M is the length of the longest string
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!