Codeforces 520A Pangram 全字母句

難易度 : 800

英文原文如下

A word or a sentence in some language is called a pangram if all the characters of the alphabet of this language appear in it at least once. Pangrams are often used to demonstrate fonts in printing or test the output devices.

You are given a string consisting of lowercase and uppercase Latin letters. Check whether this string is a pangram. We say that the string contains a letter of the Latin alphabet if this letter occurs in the string in uppercase or lowercase.

中文翻譯

在一些語言中,要是一個單字或是一個句子包含了所有的字母(至少出現一次),它就被稱為 pangram (全字母句)。 Pangrams常用於展示印刷字體或測試輸出設備。

給予一個由小寫和大寫拉丁字母組成的字符串。請檢查這個字符串是否是一個Pangram。如果該字符串中至少包含該語言字母表中的每一個字母(不論大小寫),則稱該字符串包含拉丁字母。

輸入

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of characters in the string.

The second line contains the string. The string consists only of uppercase and lowercase Latin letters.

第一行包含了一個單獨的整數 n (1 ≤ n ≤ 100) — 字串有幾個字元。

第二行包含了那個字串,由大寫或小寫的拉丁字母所組成。

輸出

Output “YES”, if the string is a pangram and “NO” otherwise.

要是這個字串是一個 pangram 就印出 “YES”,反之則印出 “NO”。

範例

輸入12
toosmallword
輸出NO
輸入35
TheQuickBrownFoxJumpsOverTheLazyDog
輸出YES

解題思路

這一題因為輸入的很單純,只有大寫或小寫的字母

所以其實我們可以用HashSet來儲存出現過的字母並判斷總長度就可以了

當然要先將整個字串轉成大寫或是小寫,任一即可

C#解決方案

方案1 – HashSet

int n = int.Parse(Console.ReadLine()); //unused
char[] s = Console.ReadLine().ToLower().ToCharArray();

HashSet<char> hSet = new HashSet<char>();

foreach(char c in s){
    hSet.Add(c);
}

Console.WriteLine(hSet.Count == 26 ? "YES" : "NO");

利用 HashSet 相同物件不會重複的特性,我們可以確定在 foreach 後出現的就是總共有幾個字母出現過

Solution1.1 – 先檢查數量

int n = int.Parse(Console.ReadLine()); 

if(n<26){
    Console.WriteLine("NO");
}
else{
    char[] s = Console.ReadLine().ToLower().ToCharArray();
    
    HashSet<char> hSet = new HashSet<char>();
    
    foreach(char c in s){
        hSet.Add(c);
    }
    
    Console.WriteLine(hSet.Count == 26 ? "YES" : "NO");   
}

修正版,新增了判斷長度是否小於 26 ,因為如此一來根本不可能涵蓋英文的全部字母


結論

網站2023的年度回顧,大家有興趣可以移駕過去看看 ➡2023年度網站回顧

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持 (幫我多多分享也大感謝!!)

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 520A – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *