LeetCode 1065. 字符串的索引对
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 1065. 字符串的索引对
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
給出 字符串 text 和 字符串列表 words, 返回所有的索引對 [i, j] 使得在索引對范圍內的子字符串 text[i]…text[j](包括 i 和 j)屬于字符串列表 words。
示例 1: 輸入: text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"] 輸出: [[3,7],[9,13],[10,17]]示例 2: 輸入: text = "ababa", words = ["aba","ab"] 輸出: [[0,1],[0,2],[2,3],[2,4]] 解釋: 注意,返回的配對可以有交叉,比如,"aba" 既在 [0,2] 中也在 [2,4] 中提示: 所有字符串都只包含小寫字母。 保證 words 中的字符串無重復。 1 <= text.length <= 100 1 <= words.length <= 20 1 <= words[i].length <= 50 按序返回索引對 [i,j](即,按照索引對的第一個索引進行排序,當第一個索引對相同時按照第二個索引對排序)。來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/index-pairs-of-a-string
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
2. 解題
class Solution { public:vector<vector<int>> indexPairs(string text, vector<string>& words) {int i, len, maxlen = 0;unordered_set<string> s;for(i = 0; i < words.size(); ++i){s.insert(words[i]);maxlen = max(maxlen, (int)words[i].size());}vector<vector<int>> ans;for(i = 0; i < text.size(); ++i){for(len = 1; len <= maxlen && i+len-1 < text.size(); ++len){if(s.find(text.substr(i,len))!=s.end())ans.push_back({i,i+len-1});}}return ans;} };28 ms 10.4 MB
長按或掃碼關注我的公眾號,一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的LeetCode 1065. 字符串的索引对的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 466. 统计重复个数
- 下一篇: LeetCode 917. 仅仅反转字母