Сложно Решено: 0 Процент решений: 0%

Word Break

Decide whether text can be split into dictionary words.

Условие

Decide whether text can be split into a sequence of words from words. Words may be reused. Dynamic programming over prefixes solves it efficiently.

Implement can_break(text, words).

Input

text: Lowercase letters only. words: The dictionary; each word has 1–50 lowercase letters.

Output

Return true if a split exists.

Ограничения

0 ≤ length of text ≤ 5,000; 1 ≤ words ≤ 1,000

Примеры

Пример 1
Ввод:
text = "applepenapple"
words = ["apple", "pen"]

Ожидаемый вывод:
expected = True
Ввод:
std::string text = "applepenapple";
std::vector<std::string> words = {"apple", "pen"};

Ожидаемый вывод:
bool expected = true;
Ввод:
string text = "applepenapple";
string[] words = new string[] {"apple", "pen"};

Ожидаемый вывод:
bool expected = true;

Пояснение

"apple" + "pen" + "apple", reusing "apple".

Пример 2
Ввод:
text = "catsandog"
words = ["cats", "dog", "sand", "and", "cat"]

Ожидаемый вывод:
expected = False
Ввод:
std::string text = "catsandog";
std::vector<std::string> words = {"cats", "dog", "sand", "and", "cat"};

Ожидаемый вывод:
bool expected = false;
Ввод:
string text = "catsandog";
string[] words = new string[] {"cats", "dog", "sand", "and", "cat"};

Ожидаемый вывод:
bool expected = false;

Пояснение

Every split leaves "og" or "ando" unmatched: false.

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.