Learning GoLang with me: Check if the Sentence Is Pangram

in #programming3 years ago
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: false

Constraints:
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

Delegation Service

  1. Voting Algorithm Updated to Favor those High Delegations!
  • Delegate 1000 to justyy: Link
  • Delegate 5000 to justyy: Link
  • Delegate 10000 to justyy: Link

Support me

If you like my work, please:

  1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
  2. Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
  3. 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

Sort:  

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:

!$%cdefgh&/jklmn()qrs=?'[]

Yes, you are right... this is a LC question and the:
Constraints:
1 <= sentence.length <= 1000
sentence consists of lowercase English letters.

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.031
BTC 69371.89
ETH 3768.85
USDT 1.00
SBD 3.71