长度至少为 K 的给定字符串数组中等值字符串的计数
给定一个包含N个字符串和一个整数K的数组arr[] ,任务是找出长度至少为K的等值字符串的数量。
例子:
Input: arr[] = {“abcd”, “der”, “erty”}, K = 4
Output: 2
Explanation: All given strings are isograms, but only “abcd” and “erty” are of length at least K. Hence count is 2
Input: arr[] = {“ag”, “bka”, “lkmn”, “asdfg”}, K = 2
Output: 4
Explanation: All the strings are isograms and each strings is of length >=K. Hence count is 4.
方法:这个问题可以通过存储所有字符的频率或使用Set数据结构来解决。如果该字符串中没有任何字母出现多次,则该字符串是等值线。请按照以下步骤解决给定的问题。
- 遍历字符串数组arr[] ,并为每个字符串
- 创建字符的频率图。
- 只要任何字符的频率大于1 ,或者如果字符串的长度小于K ,则跳过当前字符串。
- 否则,如果没有字符的频率大于1 ,并且字符串的长度小于K ,则增加答案的计数。
- 当遍历所有字符串时,打印存储在答案中的计数。
下面是上述方法的实现。
C++
// C++ code for the above approach
#include
using namespace std;
// Function to check if a string
// is an isogram or not
bool isIsogram(string s)
{
// To store the frequencies
vector freq(26, 0);
for (char c : s) {
freq++;
if (freq > 1) {
return false;
}
}
return true;
}
// Function to check if array arr contains
// all isograms or not
int allIsograms(vector& arr, int K)
{
int ans = 0;
for (string x : arr) {
if (isIsogram(x) && x.length() >= K) {
ans++;
}
}
return ans;
}
// Driver Code
int main()
{
vector arr = { "abcd", "der", "erty" };
int K = 4;
// Function call and printing the answer
cout << allIsograms(arr, K);
}
Java
// Java code for the above approach
import java.io.*;
class GFG {
// Function to check if a string
// is an isogram or not
static boolean isIsogram(String s)
{
// To store the frequencies
int[] freq = new int[26];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
freq++;
if (freq > 1) {
return false;
}
}
return true;
}
// Function to check if array arr contains
// all isograms or not
static int allIsograms(String[] arr, int K)
{
int ans = 0;
for (String x : arr) {
if (isIsogram(x) && x.length() >= K) {
ans++;
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
String arr[] = { "abcd", "der", "erty" };
int K = 4;
// Function call and printing the answer
System.out.println(allIsograms(arr, K));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python code for the above approach
# Function to check if a string
# is an isogram or not
def isIsogram (s):
# To store the frequencies
freq = [0] * 26
for c in s:
freq[ord(c) - ord("a")] += 1
if (freq[s.index(c)] > 1):
return False
return True
# Function to check if array arr contains
# all isograms or not
def allIsograms (arr, K):
ans = 0
for x in arr:
if isIsogram(x) and len(x) >= K:
ans += 1
return ans
# Driver Code
arr = ["abcd", "der", "erty"]
K = 4
# Function call and printing the answer
print(allIsograms(arr, K))
# This code is contributed by Saurabh jaiswal
C#
// C# code for the above approach
using System;
class GFG{
// Function to check if a string
// is an isogram or not
static bool isIsogram(string s)
{
// To store the frequencies
int[] freq = new int[26];
for(int i = 0; i < s.Length; i++)
{
char c = s[i];
freq++;
if (freq > 1)
{
return false;
}
}
return true;
}
// Function to check if array arr contains
// all isograms or not
static int allIsograms(string[] arr, int K)
{
int ans = 0;
foreach(string x in arr)
{
if (isIsogram(x) && x.Length >= K)
{
ans++;
}
}
return ans;
}
// Driver Code
public static void Main(string[] args)
{
string[] arr = { "abcd", "der", "erty" };
int K = 4;
// Function call and printing the answer
Console.WriteLine(allIsograms(arr, K));
}
}
// This code is contributed by ukasp
Javascript
输出
2
时间复杂度: O(N*M),其中N是数组的大小, M是最长字符串的大小。
辅助空间: O(1)。