speech = {"make", "topcoder", "great", "again"} words = {"make", "america", "great", "again"} return value: 3
speech = {"toads"} words = {"toad"} return value: 0
speech = {"a", "a"} words = {"a"} return value: 2
speech = {"je", "le", "ai", "deja", "vu", "et", "je", "le", "veux", "encore"} words = {"i", "am", "having", "deja", "vu", "please", "stop", "the", "encore"} return value: 3
Instead, insert the words in words into a set, and then for each word in speech(), try to find it in the set. Now your solution is O(n log n), and all is alsmost right with the universe.
Instead, substitute an unordered_set for the set, and your solution is O(n). Now all is indeed right with the universe.