DIY: Word Break

Solve the interview question "Word Break" yourself in this lesson.

We'll cover the following

Problem statement

You are given a non-empty string s and an array of strings called subs. The subs array will contain a unique set of strings. Your job is to determine if s can be broken down into a space-separated sequence of one or more strings from the subs array. A single string from subs can be reused multiple times in the breakdown of s.

Input

The first input will be a non-empty string called s, and the second input will be a array string called subs. The following is an example input:

magically
["ag", "al", "icl", "mag", "magic", "ly", "lly"]

Output

The output will be a Boolean that represents if the string s can be broken down into substrings from subs. The following is an example output:

true

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.