PROBLEM LINK:
Author: samhenry97
Editorialist: samhenry97
DIFFICULTY:
Cakewalk
PREREQUISITES:
PROBLEM:
Count the number of certain types of tokens in given text.
QUICK EXPLANATION:
Run through using a state machine to count the tokens.
EXPLANATION:
We can use a state machine or regular expressions to find identifiers. The regular expression for an identifier is [_a-zA-Z][_a-zA-Z0-9]*. Once an identifier is found, test if it is a keyword. We use a map or dictionary to keep track of the counts of each item. If the item is a keyword, add one to the count. If the item is an identifier, add one to the identifier count.
At the end, we just print everything that we’ve totaled up.