HackerRank ‘Pangrams’ Solution

Short Problem Definition:

Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence “The quick brown fox jumps over the lazy dog” repeatedly, because it is a pangram. (Pangrams are sentences constructed by using every letter of the alphabet at least once.)

Link

Pangrams

Complexity:

time complexity is O(N)

space complexity is O(N)

Execution:

There are 26 letters in the English alphabet. A sentence is a pangram, if it contains all 26 characters.

Solution:
#!/usr/bin/py

def getCharCnt(s):
    return len(set(c.lower() for c in s if c != ' '))

if __name__ == '__main__':
    s = raw_input()
    if getCharCnt(s) == 26:
        print "pangram"
    else:
        print "not pangram"

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

Facebooktwittergoogle_plusredditpinterestlinkedin
  • Geekygeek

    Hi! Can you pls explain the given code ? thanks!
    🙂

  • There was a question by Geekygeek, how the code works.

    The function getCharCnt creates a set of all characters that are in the sentence and discards all empty spaces. Maybe you should actually discard all non-characters (0-9, ., 😉 etc. The size of the set is the number of different characters that occur in the sentence. And well, because there are 26 distinct characters in the English alphabet, you require them all for a sentence to be a pangram.