📌  相关文章
📜  检查所有给定的字符串是否都是等值线

📅  最后修改于: 2022-05-13 01:57:08.287000             🧑  作者: Mango

检查所有给定的字符串是否都是等值线

给定一个包含N个字符串的数组arr ,任务是检查所有字符串是否都是等图线。如果是,则打印Yes ,否则打印No

例子:

方法:如果字符串字符串等值线。现在要解决这个问题,

  • 遍历数组arr ,并为每个字符串
  • 创建字符的频率图。
  • 无论任何字符的频率大于 1,打印No并返回。
  • 否则,遍历整个数组后,打印Yes

下面是上述方法的实现:

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)
{
    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
bool allIsograms(vector& arr)
{
    for (string x : arr) {
        if (!isIsogram(x)) {
            return false;
        }
    }
 
    return true;
}
 
// Driver Code
int main()
{
    vector arr = { "abcd", "derg", "erty" };
    if (allIsograms(arr)) {
        cout << "Yes";
        return 0;
    }
    cout << "No";
}


Java
// Java program 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)
    {
        int freq[] = new int[26];
        char S[] = s.toCharArray();
        for (char c : S) {
            freq++;
            if (freq > 1) {
                return false;
            }
        }
 
        return true;
    }
 
    // Function to check if array arr contains
    // all isograms or not
    static boolean allIsograms(String arr[])
    {
        for (String x : arr) {
            if (isIsogram(x) == false) {
                return false;
            }
        }
 
        return true;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String arr[] = { "abcd", "derg", "erty" };
        if (allIsograms(arr) == true) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}
 
 // 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):
    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 check if array arr contains
# all isograms or not
def allIsograms (arr):
    for x in arr:
        if (not isIsogram(x)):
            return False
    return True
 
# Driver Code
arr = ["abcd", "derg", "erty"]
if (allIsograms(arr)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
 
public class GFG {
 
    // Function to check if a string
    // is an isogram or not
    static bool isIsogram(String s)
    {
        int []freq = new int[26];
        char []S = s.ToCharArray();
        foreach (char c in S) {
            freq++;
            if (freq > 1) {
                return false;
            }
        }
 
        return true;
    }
 
    // Function to check if array arr contains
    // all isograms or not
    static bool allIsograms(String []arr)
    {
        foreach (String x in arr) {
            if (isIsogram(x) == false) {
                return false;
            }
        }
 
        return true;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String []arr = { "abcd", "derg", "erty" };
        if (allIsograms(arr) == true) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
}
 
// This code is contributed by 29AjayKumar


Javascript



输出
Yes

时间复杂度: O(N*M),其中 N 是数组的大小,M 是最长字符串的大小
辅助空间: O(1)