Difficult:Medium
前言
Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
翻譯
給定一個字符串 s 和一個字符串字典 wordDict,如果 s 可以分割成一個或多個字典單詞的空格分隔序列,則返回 true。
請注意,字典中的同一個詞可能會在分詞中重複使用多次。
範例
Exampele 1:
1 | Input: s = "leetcode", wordDict = ["leet","code"] |
Example 2:
1 | Input: s = "applepenapple", wordDict = ["apple","pen"] |
Example 3:
1 | Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"] |
解題思路
- str為空和set有值時回傳true
- map有值返回map裡面的值
程式碼
1 | var wordBreak = function(s, wordDict) { |