Coding questions · TikTok
Determine if a string can be segmented
This question evaluates a candidate's understanding of dynamic programming and string-processing skills, along with algorithmic complexity analysis for segmentation problems.
Given a string s and a list of strings wordDict, determine whether s can be segmented into a sequence of one or more dictionary words.
- You may reuse dictionary words multiple times.
- Return
trueif such a segmentation exists, otherwise returnfalse.
Input
s: a non-empty stringwordDict: a list of non-empty strings
Output
- Boolean indicating whether
scan be segmented.
Example
s = "leetcode",wordDict = ["leet", "code"]→trues = "catsandog",wordDict = ["cats","dog","sand","and","cat"]→false
Notes / constraints (typical interview assumptions)
1 <= len(s) <= 10^41 <= len(wordDict) <= 10^5- Total characters across
wordDictcan be large; aim for an efficient solution.
Word Break Segmentation
Hints
- Model object-style prompts as operation streams when needed.
- Handle empty and boundary cases before the main logic.