Khó 0 đã giải Tỷ lệ chấp nhận 0%

Word Break

Decide whether text can be split into dictionary words.

Đề bài

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.

Ràng buộc

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

Ví dụ

Ví dụ 1
Đầu vào:
text = "applepenapple"
words = ["apple", "pen"]

Kết quả mong đợi:
expected = True
Đầu vào:
std::string text = "applepenapple";
std::vector<std::string> words = {"apple", "pen"};

Kết quả mong đợi:
bool expected = true;
Đầu vào:
string text = "applepenapple";
string[] words = new string[] {"apple", "pen"};

Kết quả mong đợi:
bool expected = true;

Giải thích

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

Ví dụ 2
Đầu vào:
text = "catsandog"
words = ["cats", "dog", "sand", "and", "cat"]

Kết quả mong đợi:
expected = False
Đầu vào:
std::string text = "catsandog";
std::vector<std::string> words = {"cats", "dog", "sand", "and", "cat"};

Kết quả mong đợi:
bool expected = false;
Đầu vào:
string text = "catsandog";
string[] words = new string[] {"cats", "dog", "sand", "and", "cat"};

Kết quả mong đợi:
bool expected = false;

Giải thích

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.