-
[LeetCode] Word Break알고리즘 2019. 12. 19. 03:38
문제
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"] Output: true Explanation: Return true because "leetcode" can be segmented as "leet code".
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"] Output: true Explanation: Return true because "applepenapple" can be segmented as "apple pen apple". Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] Output: false
Approach
DP를 활용한다.
메모이제이션 할 부분은 주어진 스트링 s를 i까지 읽었을 때 단어들로 스트링을 채울 수 있으면 1, 아니면 0
최종적으로 스트링을 끝까지 다 읽었을 때 단어들로 스트링을 채울 수 있으면 가능, 없으면 불가능이다
Code
https://github.com/chi3236/algorithm/blob/master/LeetCode_WordBreak.cpp
chi3236/algorithm
Contribute to chi3236/algorithm development by creating an account on GitHub.
github.com
'알고리즘' 카테고리의 다른 글
[LeetCode] Combination Sum (0) 2020.05.04 [LeetCode] Word Break II (0) 2019.12.19 [LeetCode] Copy List with Random Pointer (0) 2019.12.18 [LeetCode] Gas Station (0) 2019.12.13 [LeetCode] Palindrome Partitioning (0) 2019.12.07