Learning GoLang with me: Check if the Sentence Is Pangram
A pangram is a sentence where every letter of the English alphabet appears at least once. Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.Example 1:
Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
Output: true
Explanation: sentence contains at least one of every letter of the English alphabet.Example 2:
Input: sentence = "leetcode"
Output: falseConstraints:
1 <= sentence.length <= 1000
sentence consists of lowercase English letters.Hints:
Iterate over the string and mark each character as found (using a boolean array, bitmask, or any other similar way).
Check if the number of found characters equals the alphabet length.
GoLang: Pangram Algorithm
Using a map with key=rune (alias for character), we mark seen characters in the hash map. It is a Pangram if the size of the map is 26.
func checkIfPangram(sentence string) bool { if len(sentence) < 26 { return false } var data = make(map[rune]bool) for _, i := range sentence { data[i] = true } return len(data) == 26 }
Using fancy syntax the channel and goroutine in GoLang:
func checkIfPangram(sentence string) bool { var ans = make(chan bool) go func() { if len(sentence) < 26 { ans <- false return } var data = make(map[rune]bool) for _, i := range sentence { data[i] = true } ans <- len(data) == 26 }() return <-ans }
The channel is a pipe where you can write to and read from. The go func starts a co-routine.
See also: Teaching Kids Programming – Check if the Sentence Is Pangram
Repost to Blog
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
NEW! Following my Trail (Upvote or/and Downvote)
Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com
My contributions
- Video Downloader
- Steem Blockchain Tools
- Free Cryptos API
- VPS Database
- Computing Technology Blog
- A few useless tools
- And some other online software/tools
- Merge Files/Videos
- LOGO Turtle Programming Chrome Extension
- Teaching Kids Programming - Youtube Channel and All Contents
Delegation Service
Support me
If you like my work, please:
- Buy Me a Coffee, Thanks!
- Become my Sponsor, Thanks!
- Voting for me:
https://steemit.com/~witnesses type in justyy and click VOTE
- Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
- Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
- Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses
Actually, the code checks if the string contains 26 different runes. That is, I can pick any 26 different runes and make the function return true.
E.g., it says true with
the29ick3roønfoxj9mpsoverthelazydo1
which isn't a pangram, of course.And it also say true with
∧≠€∴þ←↓→ø¶æßð∂ŋ…–—λ«»÷“”ñµ
as well (and if you print the len of this, you'll get a surprise: it isn't 26…)To test it properly, you can start with only the 26 different letters, abcdefghijklmnopqrstuvwxyz
and then replace with different symbols (in the ASCII set just to avoid confusion with UTF-8 codes); e.g., the following is a pangram, too, according to your naive algorithm:
Yes, you are right... this is a LC question and the:
Constraints:
1 <= sentence.length <= 1000
sentence consists of lowercase English letters.