Module 1 Supplement : Pangram


The Obvious Strategy

There are a variety of strategies that we can use to solve the Pangram problem, so first consider your first instinct to the problem.

The most straightforward strategy/algorithm looks something like this:


Analysis of the Obvious Strategy

While this is a perfectly fine algorithm that accomplishes the goal, it is not an optimal approach to the problem. Take a step back and generalize this algorithm such that it searches for some list of symbols in an arbitrary sequence of symbols. If our list of symbols or our the sequence in which we search grow larger, the algorithm will become significantly slower because we are nesting a loop within another loop. Can we quantify this so that we have an objective measure of how 'fast' the program is?

We can approximate how many operations the program will take by multiplying togther the number of symbols by the length of the sequence. For our pangram, this would be 26n where n is the length of the string sequence. For our abstract problem, it would be mn where m is the number of symbols in our 'alphabet' and n is the length of our string.

So, is there a faster way?


The Fast Strategy

This strategy will scan through the string only once:


Analysis of the Fast Strategy

Why is this a faster algorithm? We have broken the problem up into a number of seperate, individual tasks that we can perfom quickly:

  • We scan through the string sequence only once to count the occurances of each symbol.
  • We iterate over the counts and check for a zero count
In the obvious algorithm, the task of checking a letter is nested within the outer loop that scans through the string. In this algorithm, checking the letter is an entirely seperate process that operates on the data stored in the array. As a result, this program takes 26 + n operations where n is the number of symbols in our string sequence.

If we generalize this problem as we did with the obvious algorithm, we will find that this algorithm take m + n operations where m is the number of symbols in our alphabet and n is the number of symbols in our string. m + n is much less than mn for any notable size of m or n, so this algorithm is faster than the obvious algorithm.


© 2006-2020, Rahul Simha & James Taylor (revised 2021)