Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Word break and all possible sentences



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Word Segmentation Problem

Word Segmentation Problem

Problem Statement

Given a non-empty string text and a list dictWords containing a set of non-empty words, check if text can be split into a sequence of one or more dictionary words separated by spaces.

Note:

  • Words from the dictionary can be used multiple times in the segmentation.
  • You can assume there are no duplicate words in the dictionary list.

Examples

Example 1:
Input: text = "programming", dictWords = ["pro", "gram", "ming"]
Output: true
Explanation: "programming" can be segmented as "pro gram ming".

Example 2:
Input: text = "appletreeapple", dictWords = ["apple", "tree"]
Output: true
Explanation: "appletreeapple" can be segmented as "apple tree apple".
Note that dictionary words can be reused.

Example 3:
Input: text = "catsanddogs", dictWords = ["cats", "dog", "sand", "and", "cat"]
Output: false

Constraints

  • 1 ≤ length of text ≤ 300
  • 1 ≤ number of words in dictWords ≤ 1000
  • 1 ≤ length of each word in dictWords ≤ 20