Skip to the code
cf.
+10 XP

#547 Split a Hashtag Into Known Words

easy

To read hashtags aloud, a screen reader helper tries to split a hashtag's text tag (without the #) into words from a dictionary words. Each word may be used any number of times, and every character of tag must belong to some word.

Write canSplitTag(tag, words) that returns true if such a split exists, and false otherwise.

Example 1

Input: tag = "darkmodetips", words = ["dark","mode","tips","tip"]
Output: true

dark + mode + tips.

Example 2

Input: tag = "darkmodes", words = ["dark","mode"]
Output: false

After dark + mode, the final s is not a word.

Constraints

  • 1 <= tag.length <= 300
  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 20
  • All strings are lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).