计算不包含任何大写和小写字母的字符串
给定一个包含N个字符串的字符串数组arr[] ,任务是计算不包含字母表中的大写和小写字符的字符串的数量。
例子:
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
方法:给定的问题可以使用贪心的方法来解决,方法是遍历所有给定的字符串,并为每个字母检查给定的字符串是否同时包含其大写和小写对应项。请按照以下步骤解决此问题:
- 创建一个变量计数来存储所需的计数。用0初始化它。
- 现在,遍历数组arr[]中的每个字符串,并将每个字符串的小写字符的频率存储在无序映射M中。
- 现在遍历该字符串,并检查每个大写字母的对应小写字母的频率是否大于零。如果是,则将count的值增加 1。
- 返回计数作为最终答案。
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to find count of strings that
// do not contain the uppercase and
// lowercase character of same alphabet
int countStrings(vector& 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 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 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 M = new HashMap();
// 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 M =
new Dictionary();
// 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
输出
2
时间复杂度: O(N * M),其中 M 是最长字符串的长度
辅助空间: O(1)