Substring with Concatenation of All Words 字符串操作 匹配的字串数
2015-06-25 14:13
429 查看
Substring with Concatenation of All Words
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 thatis a concatenation 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).
class Solution { public: vector<int> findSubstring(string s, vector<string>& words) { int wordLen=words.size(); vector<int> res; if(wordLen<=0) return res; int wordSize=words[0].size(); int i,j; map<string,int> wordCount; //map统计每个单词出现 for(i=0;i<wordLen;i++) wordCount[words[i]]++; map<string,int> tmp; int len=s.length(); // for(i=0;i<=s.length()-(wordLen*wordSize);i++) 刚开始s.length没有用int转换一直过不了 //改为i<=(int)s.length() 过了 for(i=0;i<=len-(wordLen*wordSize);i++) { tmp.clear(); for(j=0;j<wordLen;j++) { string str=s.substr(i+j*wordSize,wordSize); if(wordCount.find(str)!=wordCount.end()) { tmp[str]++; if(tmp[str]>wordCount[str]) break; } else break; } if(j==wordLen) res.push_back(i); } return res; } };
相关文章推荐
- 关于Code Review
- java泛型
- (总结)HTTP常见错误返回代码
- 关于easyui datebox的记录
- WPF中的TreeView如何定义模版,并绑定数据
- Android - Data Binding
- AngularJS+Servlet跨域问题
- 曝光:Windows 10移动版系统被截图
- win7旗舰版如何运行jar文件
- 6.25 被虐记 3 期末测试 const
- Eclipse 调整背景色
- 如何提升你的能力?给年轻程序猿的几条建议
- QWidget类
- python类的继承
- 如何在Windows环境下运行Python脚本
- Myeclipse8.5中svn插件安装
- MySQL主从架构配置案例
- CWE-908: Use of Uninitialized Resource
- Win7下Eclipse中文字体太小解决
- Java Reflection(五):变量