Schwierig 0 gelöst 0% Erfolgsquote

Word Break

Decide whether text can be split into dictionary words.

Problem

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.

Einschränkungen

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

Beispiele

Beispiel 1
Eingabe:
text = "applepenapple"
words = ["apple", "pen"]

Erwartete Ausgabe:
expected = True
Eingabe:
std::string text = "applepenapple";
std::vector<std::string> words = {"apple", "pen"};

Erwartete Ausgabe:
bool expected = true;
Eingabe:
string text = "applepenapple";
string[] words = new string[] {"apple", "pen"};

Erwartete Ausgabe:
bool expected = true;

Erläuterung

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

Beispiel 2
Eingabe:
text = "catsandog"
words = ["cats", "dog", "sand", "and", "cat"]

Erwartete Ausgabe:
expected = False
Eingabe:
std::string text = "catsandog";
std::vector<std::string> words = {"cats", "dog", "sand", "and", "cat"};

Erwartete Ausgabe:
bool expected = false;
Eingabe:
string text = "catsandog";
string[] words = new string[] {"cats", "dog", "sand", "and", "cat"};

Erwartete Ausgabe:
bool expected = false;

Erläuterung

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.