Skip to the code
cf.
+25 XP

#551 Fewest Leftover Characters

medium

A CSS class linter reads a long class string text with the spaces stripped out and tries to cover it with known utility class names from words. Pieces must not overlap, each word may be used many times, and characters not covered by any word are leftovers.

Write fewestLeftovers(text, words) that returns the smallest possible number of leftover characters.

Example 1

Input: text = "navbarxlink", words = ["nav","bar","link","navbar"]
Output: 1

navbar + x + link: only the x is left over.

Example 2

Input: text = "abc", words = ["d"]
Output: 3

No word appears, so every character is left over.

Constraints

  • 1 <= text.length <= 1000
  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 30
  • 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).