GoLang: 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: 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.

1
2
3
4
5
6
7
8
9
10
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
}
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
}
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.

The constraints are that the input strings are already lowercase characters, if we want, we can always double check that the characters to see if they are uppercase or lowercase (unicode.IsUpper or unicode.IsLower to check if a rune is uppercase or lowercase character):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 {
            if !(unicode.IsUpper(i)|| unicode.IsLower(i)) { // not a alphabet letter
                ans <- false
                return
            }
            data[i] = true
        }
        ans <- len(data) == 26        
    }()
    return <-ans
}
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 {
            if !(unicode.IsUpper(i)|| unicode.IsLower(i)) { // not a alphabet letter
                ans <- false
                return
            }
            data[i] = true
        }
        ans <- len(data) == 26        
    }()
    return <-ans
}

See also: Teaching Kids Programming – Check if the Sentence Is Pangram

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
463 words
Last Post: Teaching Kids Programming - Maximum Number by Inserting Five
Next Post: Teaching Kids Programming - Max Product of Two Numbers

The Permanent URL is: GoLang: Check if the Sentence Is Pangram

Leave a Reply