문제
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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.