给定字符串数组中的等值线字符串计数
给定一个包含N个字符串的数组arr[] ,任务是找出等值图的字符串数。如果该字符串中没有任何字母出现多次,则该字符串是等值线。
例子:
Input: arr[] = {“abcd”, “derg”, “erty”}
Output: 3
Explanation: All given strings are isograms. In all the strings no character
is present more than once. Hence count is 3
Input: arr[] = {“agka”, “lkmn”}
Output: 1
Explanation: Only string “lkmn” is isogram. In the string “agka”
the character ‘a’ is present twice. Hence count is 1.
方法:贪心方法可以用来解决这个问题。遍历给定字符串数组中的每个字符串并检查它是否是等图线。为此,请按照以下步骤操作:
- 遍历字符串数组并对每个字符串执行以下步骤:
- 创建字符的频率图。
- 无论任何字符的频率大于 1 ,跳过当前字符串并移至下一个字符串。
- 如果没有字符的频率超过 1,则将答案计数加1。
- 当遍历所有字符串时,返回存储在 answer 中的计数。
下面是上述方法的实现:
C++
#include
using namespace std;
// Function to check
// if a string is an isogram
bool isIsogram(string s)
{
// Loop to check
// if string is isogram or not
vector freq(26, 0);
for (char c : s) {
freq++;
if (freq > 1) {
return false;
}
}
return true;
}
// Function to count the number of isograms
int countIsograms(vector& arr)
{
int ans = 0;
// Loop to iterate the string array
for (string x : arr) {
if (isIsogram(x)) {
ans++;
}
}
return ans;
}
// Driver Code
int main()
{
vector arr = { "abcd", "derg", "erty" };
// Count of isograms in string array arr[]
cout << countIsograms(arr) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.ArrayList;
class GFG {
// Function to check
// if a String is an isogram
static boolean isIsogram(String s) {
// Loop to check
// if String is isogram or not
int[] freq = new int[26];
for (int i = 0; i < 26; i++) {
freq[i] = 0;
}
for (char c : s.toCharArray()) {
freq++;
if (freq > 1) {
return false;
}
}
return true;
}
// Function to count the number of isograms
static int countIsograms(ArrayList arr) {
int ans = 0;
// Loop to iterate the String array
for (String x : arr) {
if (isIsogram(x)) {
ans++;
}
}
return ans;
}
// Driver Code
public static void main(String args[]) {
ArrayList arr = new ArrayList();
arr.add("abcd");
arr.add("derg");
arr.add("erty");
// Count of isograms in String array arr[]
System.out.println(countIsograms(arr));
}
}
// This code is contributed by gfgking
Python3
# Function to check
# if a string is an isogram
def isIsogram(s):
# Loop to check
# if string is isogram or not
freq = [0]*(26)
for c in s:
freq[ord(c) - ord('a')] += 1
if (freq[ord(c) - ord('a')] > 1):
return False
return True
# Function to count the number of isograms
def countIsograms(arr):
ans = 0
# Loop to iterate the string array
for x in arr:
if (isIsogram(x)):
ans += 1
return ans
# Driver Code
if __name__ == "__main__":
arr = ["abcd", "derg", "erty"]
# Count of isograms in string array arr[]
print(countIsograms(arr))
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
using System.Collections;
class GFG
{
// Function to check
// if a string is an isogram
static bool isIsogram(string s)
{
// Loop to check
// if string is isogram or not
int []freq = new int[26];
for(int i = 0; i < 26; i++) {
freq[i] = 0;
}
foreach (char c in s) {
freq++;
if (freq > 1) {
return false;
}
}
return true;
}
// Function to count the number of isograms
static int countIsograms(ArrayList arr)
{
int ans = 0;
// Loop to iterate the string array
foreach (string x in arr) {
if (isIsogram(x)) {
ans++;
}
}
return ans;
}
// Driver Code
public static void Main()
{
ArrayList arr = new ArrayList();
arr.Add("abcd");
arr.Add("derg");
arr.Add("erty");
// Count of isograms in string array arr[]
Console.WriteLine(countIsograms(arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
3
时间复杂度: O(N*M),其中 N 是数组的大小,M 是最长字符串的大小
辅助空间: O(1)