Pangrams python implementation Given


  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.)
  After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams. Given a sentence s, tell Roy if it is a pangram or not. Input Format Input consists of a line containing s.


Constraints


  Length of s can be at most 103 \$(1≤|s|≤103)\$ and it may contain spaces, lower case and upper case letters. Lower case and upper case instances of a letter are considered the same.


Solution 1

from collections import defaultdict
import string

def is_pangram(astr):
    lookup = defaultdict(int)

    for char in astr:
        lookup[char.lower()] += 1        
    for char in string.ascii_lowercase:
        if lookup[char] == 0:
            return False
    return True

print "pangram" if is_pangram(raw_input()) else "not pangram"


Solution 2

from collections import Counter
import string

def is_pangram(astr):
    counter = Counter(astr.lower())
    for char in string.ascii_lowercase:
        if counter[char] == 0:
            return False
    return True

print "pangram" if is_pangram(raw_input()) else "not pangram"


Which is better in terms of running time and space complexity?
Since you do not actually need the count of each character, it would be simpler to use a set and the superset operator &gt;=:

def is_pangram(astr):
    return set(astr.lower()) &gt;= set(string.ascii_lowercase):



Your two solutions are almost identical, though here

for char in astr:
    lookup[char.lower()] += 1  


it would be faster to lowercase the whole string at once (like you already do in solution 2):

for char in astr.lower():
    lookup[char] += 1  


Other than that, the only difference is defaultdict vs. Counter. The latter makes your code more elegant, but since the standard library implements Counter in pure Python, there may be a speed advantage to defaultdict.