Count Strings that does not contain any alphabet’s both uppercase and lowercase
Last Updated :
07 Nov, 2023
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 2.
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:
- Create a variable count to store the required count. Initialize it with 0.
- Now, traverse on each string in array arr[] and for each string store the frequency of its lowercase characters in an unordered map, M.
- 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.
- Return count as the final answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countStrings(vector<string>& arr)
{
int count = 0;
for ( auto x : arr) {
bool isAllowed = 1;
unordered_map< char , int > M;
for ( auto y : x) {
if (y - 'a' >= 0 and y - 'a' < 26) {
M[y]++;
}
}
for ( auto y : x) {
if (y - 'A' >= 0 and y - 'A' < 26
and M[ tolower (y)] > 0) {
isAllowed = 0;
break ;
}
}
if (isAllowed) {
count += 1;
}
}
return count;
}
int main()
{
vector<string> arr
= { "abcdA" , "abcd" , "abcdB" , "abc" };
cout << countStrings(arr);
}
|
Java
import java.util.*;
class GFG{
static int countStrings(String[]arr)
{
int count = 0 ;
for (String x : arr) {
boolean isAllowed = true ;
HashMap<Character,Integer> M = new HashMap<Character,Integer>();
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()) {
if (y - 'A' >= 0 && y - 'A' < 26 && M.containsKey(Character.toLowerCase(y))
&& M.get(Character.toLowerCase(y)) > 0 ) {
isAllowed = false ;
break ;
}
}
if (isAllowed) {
count += 1 ;
}
}
return count;
}
public static void main(String[] args)
{
String []arr
= { "abcdA" , "abcd" , "abcdB" , "abc" };
System.out.print(countStrings(arr));
}
}
|
Python3
def countStrings(arr):
count = 0
for x in arr:
isAllowed = 1
M = {}
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:
if ( ord (y) - ord ( 'A' ) > = 0 and ord (y) - ord ( 'A' ) < 26 and M[y.lower()] > 0 ):
isAllowed = 0
break
if (isAllowed):
count + = 1
return count
arr = [ "abcdA" , "abcd" , "abcdB" , "abc" ]
print (countStrings(arr))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static int countStrings(ArrayList arr)
{
int count = 0;
foreach ( string x in arr) {
bool isAllowed = true ;
Dictionary< char , int > M =
new Dictionary< char , int >();
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) {
if (y - 'A' >= 0 && y - 'A' < 26
&& M[Char.ToLower(y)] > 0) {
isAllowed = false ;
break ;
}
}
if (isAllowed) {
count += 1;
}
}
return count;
}
public static void Main()
{
ArrayList arr = new ArrayList();
arr.Add( "abcdA" );
arr.Add( "abcd" );
arr.Add( "abcdB" );
arr.Add( "abc" );
Console.Write(countStrings(arr));
}
}
|
Javascript
<script>
function countStrings(arr)
{
let count = 0;
for (let x of arr)
{
let isAllowed = 1;
let M = new Map();
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];
if (y.charCodeAt(0) - 'A' .charCodeAt(0) >= 0 && y.charCodeAt(0) - 'A' .charCodeAt(0) < 26
&& M.get(y.toLowerCase()) > 0) {
isAllowed = 0;
break ;
}
}
if (isAllowed) {
count += 1;
}
}
return count;
}
let arr
= [ "abcdA" , "abcd" , "abcdB" , "abc" ];
document.write(countStrings(arr));
</script>
|
Time Complexity: O(N * M), where M is the length of the longest string
Auxiliary Space: O(1)
Approach:
- Iterate through each string in the array.
- For each string, check if it contains at least one uppercase and one lowercase character using two boolean flags.
- If the string does not contain both uppercase and lowercase characters, increment the count of valid strings.
- Return the count of valid strings.
Steps:
- Initialize a variable count to 0 to keep track of the number of valid strings.
- Loop through each string in the given array.
- For each string, initialize two boolean flags, one for uppercase and one for lowercase.
- Loop through each character in the string and set the corresponding flag to True if an uppercase or lowercase character is found.
- If the string does not contain both uppercase and lowercase characters, increment the count of valid strings.
- Return the count of valid strings.
C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int count_strings_without_upper_lower(vector<string> arr) {
int count = 0;
for (string str : arr) {
bool has_upper = false ;
bool has_lower = false ;
for ( char c : str) {
if ( isupper (c)) {
has_upper = true ;
} else if ( islower (c)) {
has_lower = true ;
}
if (has_upper && has_lower) {
break ;
}
}
if (!has_upper || !has_lower) {
count++;
}
}
return count;
}
int main() {
vector<string> arr = { "abcdA" , "abcd" , "abcdB" , "abc" };
cout << count_strings_without_upper_lower(arr) << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static int countStringsWithoutUpperLower(List<String> arr) {
int count = 0 ;
for (String str : arr) {
boolean hasUpper = false ;
boolean hasLower = false ;
for ( char c : str.toCharArray()) {
if (Character.isUpperCase(c)) {
hasUpper = true ;
} else if (Character.isLowerCase(c)) {
hasLower = true ;
}
if (hasUpper && hasLower) {
break ;
}
}
if (!hasUpper || !hasLower) {
count++;
}
}
return count;
}
public static void main(String[] args) {
List<String> arr = new ArrayList<>();
arr.add( "abcdA" );
arr.add( "abcd" );
arr.add( "abcdB" );
arr.add( "abc" );
System.out.println(countStringsWithoutUpperLower(arr));
}
}
|
Python3
def count_strings_without_upper_lower(arr):
count = 0
for string in arr:
has_upper = False
has_lower = False
for char in string:
if char.isupper():
has_upper = True
elif char.islower():
has_lower = True
if has_upper and has_lower:
break
else :
count + = 1
return count
arr = [ "abcdA" , "abcd" , "abcdB" , "abc" ]
print (count_strings_without_upper_lower(arr))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int CountStringsWithoutUpperLower(List< string > arr)
{
int count = 0;
foreach ( string str in arr)
{
bool hasUpper = false ;
bool hasLower = false ;
foreach ( char c in str)
{
if (Char.IsUpper(c))
{
hasUpper = true ;
}
else if (Char.IsLower(c))
{
hasLower = true ;
}
if (hasUpper && hasLower)
{
break ;
}
}
if (!hasUpper || !hasLower)
{
count++;
}
}
return count;
}
static void Main( string [] args)
{
List< string > arr = new List< string > { "abcdA" , "abcd" , "abcdB" , "abc" };
Console.WriteLine(CountStringsWithoutUpperLower(arr));
}
}
|
Javascript
function countStringsWithoutUpperLower(arr) {
let count = 0;
for (let str of arr) {
let hasUpper = false ;
let hasLower = false ;
for (let c of str) {
if (c.toUpperCase() === c) {
hasUpper = true ;
} else if (c.toLowerCase() === c) {
hasLower = true ;
}
if (hasUpper && hasLower) {
break ;
}
}
if (!hasUpper || !hasLower) {
count++;
}
}
return count;
}
const arr = [ "abcdA" , "abcd" , "abcdB" , "abc" ];
console.log(countStringsWithoutUpperLower(arr));
|
Time Complexity: O(N*M) where N is the length of the array and M is the length of the longest string in the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...