leetcode 30. Substring with Concatenation of All Words
来源:互联网 发布:土耳其人 知乎 编辑:程序博客网 时间:2024/05/01 05:49
题目:
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) ins that is a concatenation of each word in words exactly once and without any intervening characters.
在s中找到words中所有字符串连接(连接顺序无关)起来的索引下标。
For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]
相当于,可以foobar 或者 barfoo 这两种连接方式,那么barfoo 索引是0,foobar索引是9
You should return the indices: [0,9]
.
(order does not matter).
解答:
可以这样考虑,可以得到words总共的字符串长度,(本例子为6),在s中依次找连续6位的字符序列,每三位每三位统计该字符串是否在words中出现过,并且统计出现的次数。如果其中每三位的字符序列与words中的某个字符串相同,且出现次数一样,那么认为这6位字符序列复合要找的格式。
class Solution {public: vector<int> findSubstring(string s, vector<string>& words) { if(words.empty()) return vector<int>(); //返回vector类型,保存找到的indices vector<int> ret; //s长度 int s_len=s.size(); //words长度 int words_num=words.size(); //words内,字符串长度 int words_len=words[0].size(); //构建一个map类型的word_count,记录words中每个字符串出现的次数 //因为题目中,words中字符串检查,不涉及出现的前后拼接顺序 //foobar,barfoo 都是允许的 map<string,int> words_counts; for(int i=0;i<words_num;i++) { words_counts[words[i]]++; } int i,j; //counts_tem用来累计在s中,找到words中每个字符串的个数 map<string,int> counts_tem; for(i=0; i<=s_len - words_num*words_len ; i++) { counts_tem.clear(); for(j=0 ; j<words_num ; j++) { //利用string的一个函数substr, //basic_string substr( size_type index, size_type num = npos ); //substr()返回本字符串的一个子串,从index开始,长num个字符。 string word=s.substr(i + j*words_len , words_len); //find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器, //如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器 if( words_counts.find(word) != words_counts.end() ) { counts_tem[word]++; if( counts_tem[word] > words_counts[word]) break; } else break; } if(j==words_num) { ret.push_back(i); } } return ret; }};
0 0
- LeetCode: Substring with Concatenation of All Words
- LeetCode : Substring with Concatenation of All Words
- [Leetcode] Substring with Concatenation of All Words
- [LeetCode]Substring with Concatenation of All Words
- LeetCode-Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- LeetCode:Substring with Concatenation of All Words
- Leetcode: Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- LeetCode Substring with Concatenation of All Words
- LeetCode | Substring with Concatenation of All Words
- LeetCode - Substring with Concatenation of All Words
- Substring with Concatenation of All Words -- LeetCode
- Leetcode: Substring with Concatenation of All Words
- leetcode:Substring with Concatenation of All Words
- LeetCode|Substring with Concatenation of All Words
- Swagger
- Java语言特性备忘
- 苹果个人公司类型开发者账号申请(99美元)详解
- java多线程
- WIN7 X64 如何删除计算机管理里面一些无效的服务
- leetcode 30. Substring with Concatenation of All Words
- easyui如何展现被选中的关联数据
- WebViewJavascriptBridge详细使用
- 制作简单中转的定时器[settimeout]
- 剑指offer面试题-求整数二进制中1的个数
- 开启软件开发之路
- RabbitMQ (三) 发布/订阅
- SpringBoot之Hypermedia-Driven RESTful Web Service
- 【原创】增强版冒泡排序算法-java版