给定一个由小写英文字母组成的字符串数组。任务是对每个字母 [az] 找出具有这些字母的字符串的数量。
例子:
Input: str = { “geeks”, “for”, “code” }
Output: { 0 0 1 1 2 1 1 0 0 0 0 0 0 0 2 0 0 1 1 0 0 0 0 0 0 0 }
Explanation:
For a letter, say ‘e’, it is present in { “geeks”, “code” }, hence its count is 2.
Similarly for another letter result can be found.
Input: str = { “i”, “will”, “practice”, “everyday” }
Output: 2 0 1 1 2 0 0 0 3 0 0 0 0 0 0 1 0 2 0 1 0 1 1 0 1 0
Explanation:
For a letter, say ‘i’, it is present in { “i”, “will”, “practice” }, hence its count is 3.
Similarly for another letter result can be found.
天真的方法:
- 为每个大小为 26 的字母创建一个全局计数器,将其初始化为 0。
- 为每个小写字母的英文字母运行一个循环。如果在当前字符串找到当前字母,则增加字母“x”的计数器。
- 对所有小写英文字母重复此操作。
时间复杂度: O(26*N),其中 N 是所有字符串的长度之和。
有效的方法:
- 而不是为每个小写英文字母运行循环并检查它是否存在于当前字符串。我们可以改为对每个字符串单独运行一个循环,并为该字符串存在的任何字母增加全局计数器。
- 此外,为了避免重复计数,我们创建了一个访问过的布尔数组来标记到目前为止遇到的字符。这种方法将复杂度降低到 O(N)。
下面是上述方法的实现:
C++
// C++ program to find count of strings
// for each letter [a-z] in english alphabet
#include
using namespace std;
// Function to find the countStrings
// for each letter [a-z]
void CountStrings(vector& str)
{
int size = str.size();
// Initialize result as zero
vector count(26, 0);
// Mark all letter as not visited
vector visited(26, false);
// Loop through each strings
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < str[i].length(); ++j)
{
// Increment the global counter
// for current character of string
if (visited[str[i][j]] == false)
count[str[i][j] - 'a']++;
visited[str[i][j]] = true;
}
// Instead of re-initialising boolean
// vector every time we just reset
// all visited letter to false
for (int j = 0; j < str[i].length(); ++j)
{
visited[str[i][j]] = false;
}
}
// Print count for each letter
for (int i = 0; i < 26; ++i)
{
cout << count[i] << " ";
}
}
// Driver program
int main()
{
// Given array of strings
vector str = {"i", "will",
"practice", "everyday"};
// Call the countStrings function
CountStrings(str);
return 0;
}
Java
// Java program to find count of Strings
// for each letter [a-z] in english alphabet
class GFG{
// Function to find the countStrings
// for each letter [a-z]
static void CountStrings(String []str)
{
int size = str.length;
// Initialize result as zero
int []count = new int[26];
// Mark all letter as not visited
boolean []visited = new boolean[26];
// Loop through each Strings
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < str[i].length(); ++j)
{
// Increment the global counter
// for current character of String
if (visited[str[i].charAt(j) - 'a'] == false)
count[str[i].charAt(j) - 'a']++;
visited[str[i].charAt(j) - 'a'] = true;
}
// Instead of re-initialising boolean
// vector every time we just reset
// all visited letter to false
for (int j = 0; j < str[i].length(); ++j)
{
visited[str[i].charAt(j) - 'a'] = false;
}
}
// Print count for each letter
for (int i = 0; i < 26; ++i)
{
System.out.print(count[i] + " ");
}
}
// Driver program
public static void main(String[] args)
{
// Given array of Strings
String []str = {"i", "will",
"practice", "everyday"};
// Call the countStrings function
CountStrings(str);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program to find count of
# strings for each letter [a-z] in
# english alphabet
# Function to find the countStrings
# for each letter [a-z]
def CountStrings(s):
size = len(s)
# Initialize result as zero
count = [0] * 26
# Mark all letter as not visited
visited = [False] * 26
# Loop through each strings
for i in range(size):
for j in range(len(s[i])):
# Increment the global counter
# for current character of string
if visited[ord(s[i][j]) -
ord('a')] == False:
count[ord(s[i][j]) -
ord('a')] += 1
visited[ord(s[i][j]) -
ord('a')] = True
# Instead of re-initialising boolean
# vector every time we just reset
# all visited letter to false
for j in range(len(s[i])):
visited[ord(s[i][j]) -
ord('a')] = False
# Print count for each letter
for i in range(26):
print(count[i], end = ' ')
# Driver code
if __name__=='__main__':
# Given array of strings
s = [ "i", "will",
"practice", "everyday" ]
# Call the countStrings function
CountStrings(s)
# This code is contributed by rutvik_56
C#
// C# program to find count of Strings
// for each letter [a-z] in english alphabet
using System;
class GFG{
// Function to find the countStrings
// for each letter [a-z]
static void CountStrings(String []str)
{
int size = str.Length;
// Initialize result as zero
int []count = new int[26];
// Mark all letter as not visited
bool []visited = new bool[26];
// Loop through each Strings
for(int i = 0; i < size; ++i)
{
for(int j = 0; j < str[i].Length; ++j)
{
// Increment the global counter
// for current character of String
if (visited[str[i][j] - 'a'] == false)
count[str[i][j] - 'a']++;
visited[str[i][j] - 'a'] = true;
}
// Instead of re-initialising bool
// vector every time we just reset
// all visited letter to false
for(int j = 0; j < str[i].Length; ++j)
{
visited[str[i][j] - 'a'] = false;
}
}
// Print count for each letter
for(int i = 0; i < 26; ++i)
{
Console.Write(count[i] + " ");
}
}
// Driver code
public static void Main(String[] args)
{
// Given array of Strings
String []str = { "i", "will",
"practice", "everyday"};
// Call the countStrings function
CountStrings(str);
}
}
// This code is contributed by Amit Katiyar
2 0 1 1 2 0 0 0 3 0 0 1 0 0 0 1 0 2 0 1 0 1 1 0 1 0
时间复杂度: O(N),其中 N 是所有字符串的长度之和。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live