★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/ )
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-dozrajlt-mb.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a text
string and words
(a list of strings), return all index pairs [i, j]
so that the substring text[i]...text[j]
is in the list of words
.git
Example 1:github
Input: text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"] Output: [[3,7],[9,13],[10,17]]
Example 2:微信
Input: text = "ababa", words = ["aba","ab"] Output: [[0,1],[0,2],[2,3],[2,4]] Explanation: Notice that matches can overlap, see "aba" is found in [0,2] and [2,4].
Note:app
words
are different.1 <= text.length <= 100
1 <= words.length <= 20
1 <= words[i].length <= 50
[i,j]
in sorted order (i.e. sort them by their first coordinate in case of ties sort them by their second coordinate).給出 字符串 text 和 字符串列表 words, 返回全部的索引對 [i, j] 使得在索引對範圍內的子字符串 text[i]...text[j](包括 i 和 j)屬於字符串列表 words。
示例 1:spa
輸入: text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"]
輸出: [[3,7],[9,13],[10,17]]
示例 2:code
輸入: text = "ababa", words = ["aba","ab"]
輸出: [[0,1],[0,2],[2,3],[2,4]]
解釋:
注意,返回的配對能夠有交叉,好比,"aba" 既在 [0,2] 中也在 [2,4] 中
提示:orm
1.全部字符串都只包含小寫字母。
2.保證 words 中的字符串無重複。
3.1 <= text.length <= 100
4.1 <= words.length <= 20
5.1 <= words[i].length <= 50
6.按序返回索引對 [i,j](即,按照索引對的第一個索引進行排序,當第一個索引對相同時按照第二個索引對排序)。htm
Runtime: 92 msblog
Memory Usage: 21.4 MB
1 class Solution { 2 func indexPairs(_ text: String, _ words: [String]) -> [[Int]] { 3 var ret:[[Int]] = [[Int]]() 4 let arrText:[Character] = Array(text) 5 for word in words 6 { 7 let arrStr:[Character] = Array(word) 8 let num:Int = arrStr.count 9 for i in 0..<arrText.count 10 { 11 if (arrText[i] == arrStr.first!) && (i + num <= arrText.count) && (Array(arrText[i..<(i + num)]) == arrStr) 12 { 13 ret.append([i,i + num - 1]) 14 } 15 } 16 } 17 ret.sort(){ 18 if $0[0] == $1[0] 19 { 20 return $0[1] <= $1[1] 21 } 22 else 23 { 24 return $0[0] <= $1[0] 25 } 26 } 27 return ret 28 } 29 }