Skip to the code
cf.
+50 XP

#291 Restore the Slug Hyphens

hard

A CMS export lost the hyphens in its page slugs: dark-mode-toggle became darkmodetoggle. You have a dictionary of known words.

Write splitSlug(slug, dictionary) that returns every way to cut slug into pieces where every piece is a dictionary word. Write each way as the pieces joined with -.

Return the results sorted ascending by plain string comparison. Return [] if the slug can't be split. A dictionary word may be used any number of times.

Example 1

Input: slug = "darkmodetoggle", dictionary = ["dark","mode","darkmode","toggle"]
Output: ["dark-mode-toggle","darkmode-toggle"]

The first part can be dark then mode, or darkmode as one word. - sorts before letters.

Example 2

Input: slug = "signin", dictionary = ["sign","in","sig","nin"]
Output: ["sig-nin","sign-in"]

Both cuts use only dictionary words.

Constraints

  • 1 <= slug.length <= 20
  • 0 <= dictionary.length <= 20
  • Words and the slug are lowercase letters only. Words in dictionary are distinct.
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).