1180. Tag Harvester

Easy1.5 points estimatedStringHash Table

A social feed wants to build a list of hashtags from a post. In text, a tag is a '#' followed by a maximal run of one or more letters, digits or underscores; the run ends at the first other character or at the end of the text. A '#' that has no valid character right after it starts nothing and is ignored, and a '#' in the middle of a word still starts a tag.

Collect the tags from left to right, write each one in lowercase without its '#', drop repeats, and return the distinct tags in the order of their first appearance. If there are none, return an empty array.

A single scan with a hash set of tags already seen runs in linear time. The text is printable ASCII and may contain quotes, punctuation and backslashes.

Example 1

Input:
text = "Loving #Python and #python, also #Java!"
Output:
["python","java"]
Explanation:

#Python and #python are the same tag once lowercased, so it is kept only at its first appearance.

Example 2

Input:
text = "# alone, ## and #_x1 end#"
Output:
["_x1"]
Explanation:

A lone # with no valid character after it is ignored; the only tag is _x1.

Example 3

Input:
text = "no tags here"
Output:
[]
Explanation:

There is no #, so the result is empty.

Constraints

1 ≤ text.length ≤ 104

text is printable ASCII

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…