📜  用于检查字符串是否为 Pangram 的 C++ 程序

📅  最后修改于: 2021-10-27 16:42:23             🧑  作者: Mango

给定字符串str,任务是检查字符串是否是 pangram 或不在 C++ 中使用。

例子:

方法 1:不使用 STL

这种方法基于哈希。

  1. 创建了一个大小为 26 的 boolean 类型的哈希数据结构,索引 0 代表字符’a’,1 代表字符’b’,依此类推。
  2. 由字符遍历字符串字符和记号特定的字符作为存在于哈希。
  3. 完成字符串的遍历和标记后,遍历Hash,查看是否所有字符都存在,即每个索引都为真。如果全部被标记,则返回真,否则返回假。

下面是上述方法的实现:

C++
// 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 not
        // an alphabet, skip to next one.
        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
int main()
{
    string str = "We promptly judged"
                 " antique ivory"
                 " buckles for the next prize";
  
    if (checkPangram(str) == true)
        printf("Yes");
    else
        printf("No");
  
    return (0);
}


CPP
// C++ Program to check whether
// a string pangram or not using STL
  
#include 
using namespace std;
  
// Function to return given string
// str is pangrams yes or no
string pangrams(string s)
{
  
    // Initialization of count
    int count = 0;
  
    // Convert each letter into
    // uppercase to avoid counting
    // of both uppercase and
    // lowercase as different letters
    transform(s.begin(),
              s.end(),
              s.begin(),
              ::toupper);
  
    // Sort the string
    sort(s.begin(), s.end());
  
    // Count distinct alphabets
    for (int i = 0; i < s.size(); i++) {
        if (s[i] != s[i + 1])
            count++;
    }
    
    // If count is 27 then the string
    // contains all the alphabets
    // including space as a
    // distinct character
    if (count == 27)
        return "Yes";
  
    else
        return "No";
}
  
// Driver code
int main()
{
    // Given string str
    string str = "We promptly "
                 "judged antique"
                 "ivory buckles for "
                 "the next prize";
  
    // Function Call
    cout << pangrams(str);
  
    return 0;
}


输出
Yes

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

方法 2:使用STL

STL 的 transform() 方法可用于检查给定的字符串是否为 Pangram。

句法:

方法:
为了检查字符串包含英文字母的所有字母:

下面是上述方法的实现:

CPP

// C++ Program to check whether
// a string pangram or not using STL
  
#include 
using namespace std;
  
// Function to return given string
// str is pangrams yes or no
string pangrams(string s)
{
  
    // Initialization of count
    int count = 0;
  
    // Convert each letter into
    // uppercase to avoid counting
    // of both uppercase and
    // lowercase as different letters
    transform(s.begin(),
              s.end(),
              s.begin(),
              ::toupper);
  
    // Sort the string
    sort(s.begin(), s.end());
  
    // Count distinct alphabets
    for (int i = 0; i < s.size(); i++) {
        if (s[i] != s[i + 1])
            count++;
    }
    
    // If count is 27 then the string
    // contains all the alphabets
    // including space as a
    // distinct character
    if (count == 27)
        return "Yes";
  
    else
        return "No";
}
  
// Driver code
int main()
{
    // Given string str
    string str = "We promptly "
                 "judged antique"
                 "ivory buckles for "
                 "the next prize";
  
    // Function Call
    cout << pangrams(str);
  
    return 0;
}
输出
Yes

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

想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程