前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 1065. 字符串的索引对

LeetCode 1065. 字符串的索引对

作者头像
Michael阿明
发布2020-07-13 16:13:28
7960
发布2020-07-13 16:13:28
举报

1. 题目

给出 字符串 text 和 字符串列表 words, 返回所有的索引对 [i, j] 使得在索引对范围内的子字符串 text[i]…text[j](包括 i 和 j)属于字符串列表 words。

代码语言:javascript
复制
示例 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. 解题

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-07-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 题目
  • 2. 解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档