📜  Pangram检查

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

Pangram检查

给定一个字符串检查它是否是 Pangram。 pangram 是一个包含英文字母表中每个字母的句子。
示例:快速棕色狐狸跳过懒狗”是一个 Pangram [包含从 'a' 到 'z' 的所有字符]
“快速棕色狐狸跳过狗”不是 Pangram [不包含从 'a' 到 'z' 的所有字符,因为缺少 'l'、'z'、'y']

我们创建一个布尔类型的 mark[] 数组。我们遍历字符串的所有字符,每当我们看到一个字符时,我们都会标记它。小写和大写被认为是相同的。所以“A”和“a”在索引 0 中标记,类似地“Z”和“z”在索引 25 中标记。

在遍历所有字符后,我们检查是否所有字符都被标记。如果不是,则返回 false,因为这不是 pangram,否则返回 true。

C++
// A C++ Program to check if the given
// string is a pangram or not
#include 
using namespace std;
 
// Returns true if the string is pangram else false
bool checkPangram(string& str)
{
    // Create a hash table to mark the characters
    // present in the string
    vector mark(26, false);
 
    // For indexing in mark[]
    int index;
 
    // Traverse all characters
    for (int i = 0; i < str.length(); i++) {
 
        // If uppercase character, subtract 'A'
        // to find index.
        if ('A' <= str[i] && str[i] <= 'Z')
            index = str[i] - 'A';
 
        // If lowercase character, subtract 'a'
        // to find index.
        else if ('a' <= str[i] && str[i] <= 'z')
            index = str[i] - 'a';
 
        // If this character is other than english
        // lowercase and uppercase characters.
        else
            continue;
 
        mark[index] = true;
    }
 
    // Return false if any character is unmarked
    for (int i = 0; i <= 25; i++)
        if (mark[i] == false)
            return (false);
 
    // If all characters were present
    return (true);
}
 
// Driver Program to test above functions
int main()
{
    string str = "The quick brown fox jumps over the"
                 " lazy dog";
 
    if (checkPangram(str) == true)
        printf(" %s is a pangram", str.c_str());
    else
        printf(" %s is not a pangram", str.c_str());
 
    return (0);
}


Java
// Java Program to illustrate Pangram
class GFG {
 
    // Returns true if the string
    // is pangram else false
    public static boolean checkPangram(String str)
    {
        // Create a hash table to mark the
        // characters present in the string
        // By default all the elements of
        // mark would be false.
        boolean[] mark = new boolean[26];
 
        // For indexing in mark[]
        int index = 0;
 
        // Traverse all characters
        for (int i = 0; i < str.length(); i++) {
            // If uppercase character, subtract 'A'
            // to find index.
            if ('A' <= str.charAt(i) && str.charAt(i) <= 'Z')
                index = str.charAt(i) - 'A';
 
            // If lowercase character, subtract 'a'
            // to find index.
            else if ('a' <= str.charAt(i) && str.charAt(i) <= 'z')
 
                index = str.charAt(i) - 'a';
 
            // If this character is other than english
            // lowercase and uppercase characters.
            else
                continue;
            mark[index] = true;
        }
 
        // Return false if any character is unmarked
        for (int i = 0; i <= 25; i++)
            if (mark[i] == false)
                return (false);
 
        // If all characters were present
        return (true);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "The quick brown fox jumps over the lazy dog";
 
        if (checkPangram(str) == true)
            System.out.print(str + " is a pangram.");
        else
            System.out.print(str + " is not a pangram.");
    }
}


Python3
# A Python Program to check if the given
# string is a pangram or not
 
def checkPangram(s):
    List = []
    # create list of 26 characters and set false each entry
    for i in range(26):
        List.append(False)
         
    # converting the sentence to lowercase and iterating
    # over the sentence
    for c in s.lower():
        if not c == " ":
 
            # make the corresponding entry True
            List[ord(c) -ord('a')]= True
             
    # check if any character is missing then return False
    for ch in List:
        if ch == False:
            return False
    return True
 
# Driver Program to test above functions
sentence = "The quick brown fox jumps over the little lazy dog"
 
if (checkPangram(sentence)):
    print ('"'+sentence+'"')
    print ("is a pangram")
else:
    print ('"'+sentence+'"')
    print ("is not a pangram")
 
# This code is contributed by Danish Mushtaq


C#
// C# Program to illustrate Pangram
using System;
class GFG {
 
    // Returns true if the string
    // is pangram else false
    public static bool checkPangram(string str)
    {
 
        // Create a hash table to mark the
        // characters present in the string
        // By default all the elements of
        // mark would be false.
        bool[] mark = new bool[26];
 
        // For indexing in mark[]
        int index = 0;
 
        // Traverse all characters
        for (int i = 0; i < str.Length; i++) {
            // If uppercase character, subtract 'A'
            // to find index.
            if ('A' <= str[i] && str[i] <= 'Z')
                index = str[i] - 'A';
 
            // If lowercase character,
            // subtract 'a' to find
            // index.
            else if ('a' <= str[i] && str[i] <= 'z')
                index = str[i] - 'a';
 
            // If this character is other than english
            // lowercase and uppercase characters.
            else
                continue;
 
            mark[index] = true;
        }
 
        // Return false if any
        // character is unmarked
        for (int i = 0; i <= 25; i++)
            if (mark[i] == false)
                return (false);
 
        // If all characters
        // were present
        return (true);
    }
 
    // Driver Code
    public static void Main()
    {
        string str = "The quick brown fox jumps over the lazy dog";
 
        if (checkPangram(str) == true)
            Console.WriteLine(str + " is a pangram.");
        else
            Console.WriteLine(str + " is not a pangram.");
    }
}
 
// This code is contributed by nitin mittal.


Javascript


输出 :

"The quick brown fox jumps over the lazy dog"
 is a pangram

时间复杂度:O(n),其中 n 是字符串的长度
辅助空间 - O(1)。

https://youtu.be/Yv4ARV