easy +10 pts

Extract Hashtags

Parse a tweet and return all unique hashtags in order of appearance.

Write a function `extract_hashtags(text: str) -> list[str]` that takes a string `text` and returns a list of all unique hashtags in the order they first appear. A hashtag is defined as a '#' followed by one or more letters, digits, or underscores (`[A-Za-z0-9_]`). The hashtag includes the '#' symbol and all consecutive valid characters immediately after it. The match stops at any character that is not in the allowed set. - Only include the hashtag itself, not trailing punctuation or spaces. - Hashtags are case-sensitive; e.g., `#Python` and `#python` are considered different. - Do not count a '#' that is not followed by at least one allowed character. - Return a list of unique hashtags in the order of first appearance. Examples: - `extract_hashtags("I love #Python and #python!")` returns `["#Python", "#python"]`. - `extract_hashtags("No tags here")` returns `[]`. - `extract_hashtags("#day1 #day2 #day1")` returns `["#day1", "#day2"]`.

Constraints

- 0 ≤ len(text) ≤ 10^5 - Text consists of printable ASCII characters. - Time complexity: O(n), where n is length of text. - Your solution should handle empty strings and text with no valid hashtags.

Example

>>> extract_hashtags("I love #Python and #python!")
['#Python', '#python']
>>> extract_hashtags("No tags here")
[]
>>> extract_hashtags("#day1 #day2 #day1")
['#day1', '#day2']
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate through the string and look for '#' characters that are followed by valid characters.
When you find a valid hashtag, collect the full sequence of letters/digits/underscores after '#'.
Use a set to keep track of seen hashtags while preserving order with a list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.