[LeetCode]Substring with Concatenation of All Words
2015-11-12 15:51
555 查看
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) in s that is a concatenation
of each word in words exactly once and without any intervening characters.
For example, given:
s:
words:
You should return the indices:
(order does not matter).
找到匹配words的子串,采用hash map的方法。
采用两个hash map对比可以确定是否是子串。
of each word in words exactly once and without any intervening characters.
For example, given:
s:
"barfoothefoobarman"
words:
["foo", "bar"]
You should return the indices:
[0,9].
(order does not matter).
找到匹配words的子串,采用hash map的方法。
采用两个hash map对比可以确定是否是子串。
class Solution { public: vector<int> findSubstring(string s, vector<string>& words) { int wordlen = words[0].size(); int strlen = s.size(); vector<int> result; map<string,int> temp; map<string,int> Count; if(strlen < wordlen*words.size()) return result; for(int i=0;i<words.size();++i){ ++temp[words[i]]; } for(int i=0; i<=strlen-wordlen*words.size(); ++i){ //important bondary Count.clear(); //count the word we found int j=i; for(; j<i+wordlen*words.size(); j=j+wordlen){ string se = s.substr(j,wordlen); if(temp.count(se) != 0){ ++Count[se]; if(Count[se]>temp[se]) break; } else break; } if(j>=i+wordlen*words.size()){ result.push_back(i); } } return result; } };
相关文章推荐
- Codeforces Testing Round #12 B - Restaurant ( 贪心 )
- linux awk命令详解
- 踪视通惊艳亮相首届网络实时通信大会, 完美技术引爆全场
- 从实体Bean中给VO赋值
- Swift 2.0初探
- 块级元素
- [ecshop 资料 ] 通联支付 wap 手机端 开发帮助中心 手机支付网关 帮助中心
- AES在Android、JAVA端正常加密解密
- Shader画笔填充
- Java8 Nashorn JavaScript引擎
- 批处理查找方件中的关键词
- 快速查找算法
- No symbol table is loaded. Use the "file" command.
- 触发器和存储过程
- PowerDesigner导出为word文档
- JQuery Show()的几种效果 总有一种是你需要的
- 未知宽高元素水平垂直居中
- hdu 4442 Physical Examination
- HDU 3743 归并排序模板题
- 根据下拉列表,改变button的css和颜色