Both functions implement almost the same algorithm, but while the first does it in a simple way, the second is a little smarter and skips some unnecessary work.
check1 looks something like this:
// loop length(words1) times for each word in words1: freq1[word]++ // loop length(words2) times for each word in words2: freq2[word]++ // loop length(union(words1, words2)) times for each word in union(words1, words2): score += freq1[word] * freq2[word]
But remember: when you multiply something with zero, you get zero.
This means that counting the frequencies of words that are not included in both sets is a waste of time - we multiply the frequency by zero and add nothing to the score.
check2 takes this into account:
// loop length(words1) times for each word in words1: freq1[word]++ // loop length(words2) times for each word in words2: if freq1[word] > 0: freq2[word]++ // loop length(intersection(words1, words2)) times for each word in freq2: score += freq1[word] * freq2[word]
source share