A backcountry expedition team logs one line of trail-marker tokens every time it passes a checkpoint. The tokens are separated by single spaces, with no leading, trailing, or repeated spaces. Camp rules say a token is a properly formatted marker only if all of the following hold:
a-z), a digit (0-9), a hyphen (-), or one of the punctuation marks ., ,, !.., ,, !. If such a mark appears anywhere in the token, it must be the very last character of the token, and no other occurrence of ., ,, or ! may appear elsewhere in it.Given one day's log line, count how many of its tokens are properly formatted markers.
A single line: the log, made up of lowercase letters, digits, spaces, hyphens, and the characters ., ,, !. Tokens (maximal runs of non-space characters) are separated by exactly one space, and the line has no leading or trailing space.
A single integer: the number of tokens in the line that are properly formatted markers.
a-z, 0-9, , -, ., ,, !.Example 1
Input
base-camp marker42 relay!
Expected
3
Explanation
Every token qualifies: `base-camp` has one hyphen flanked by the letters `e` and `c`; `marker42` mixes letters and digits with no hyphen or punctuation; `relay!` ends with the single allowed punctuation mark `!` right after its letters. All three tokens are valid, so the count is 3.
Example 2
Input
north-3-east grid!! camp-
Expected
0
Explanation
`north-3-east` contains two hyphens, which is not allowed. `grid!!` ends with two punctuation marks instead of at most one. `camp-` places its only hyphen as the very last character, so it has no letter after it. None of the three tokens qualify, so the count is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →