Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reconstruct String From Vocabulary

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Reconstruct String From Vocabulary. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

NewsBreak needs to validate whether a normalized text string can be fully reconstructed from an approved vocabulary. A vocabulary word may be used any number of times, and words may share prefixes.

Given a lowercase string s and a list vocabulary, return True if s can be split into one or more contiguous vocabulary words. Return False otherwise. The empty string is considered reconstructible.

Build a solution that remains efficient when the vocabulary is large and many words share prefixes. Do not reorder characters, skip characters, or use a vocabulary word more times than allowed by the input string. The function only needs to return a boolean, not the actual segmentation.

Formal Specification

  • Input: s, a lowercase string, and vocabulary, a list of unique lowercase strings.
  • Output: A boolean indicating whether every character in s belongs to a valid vocabulary word in some segmentation.
  • Vocabulary words may be reused.

Constraints

  • 0 <= len(s) <= 100000
  • 1 <= len(vocabulary) <= 10000
  • 1 <= len(word) <= 20 for every vocabulary word
  • All characters are lowercase English letters
  • Vocabulary words are unique and may be reused

Function Signature

def can_reconstruct(s, vocabulary):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output