leetcode 30: Substring with Concatenation of All Words
2015-07-03 12:07
399 查看
I did this problem again today and tried a more efficient way. The runtime is reduced more than 10 times (32ms now). The idea is also using two unordered map to count the number of different words. But the loop now is different.
I use the start variable to loop just for the length of one word. And from this start, I check the whole string after to see if it contains a concatenation. The second loop is like a moving window.
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res;
if(s.empty()||words.empty())
return res;
int slen=s.length();
int wordLen=words[0].length();
int wordNum=words.size();
for(int i=0;i<wordNum;i++)
dict[words[i]]++;//initialize the dictionary for words
for(int start=0;start<wordLen;start++)
{
int i=start,j=start;
int count=0;
while(i<=slen-wordLen*wordNum&&j<=slen-wordLen)
{
auto it=dict.find(s.substr(j,wordLen));
if(it==dict.end())//this word is not in the dict, clear everything
{
count=0;
curr_table.clear();
i=j+wordLen;
}
else
{
curr_table[it->first]++;
count++;
while(curr_table[it->first]>it->second)
{
string temp=s.substr(i,wordLen);
curr_table[temp]--;
count--;
i+=wordLen;
}
if(count==wordNum)
res.push_back(i);
}
j+=wordLen;
}
curr_table.clear();
}
return res;
}
private:
unordered_map<string,int> dict,curr_table;
};
I use the start variable to loop just for the length of one word. And from this start, I check the whole string after to see if it contains a concatenation. The second loop is like a moving window.
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res;
if(s.empty()||words.empty())
return res;
int slen=s.length();
int wordLen=words[0].length();
int wordNum=words.size();
for(int i=0;i<wordNum;i++)
dict[words[i]]++;//initialize the dictionary for words
for(int start=0;start<wordLen;start++)
{
int i=start,j=start;
int count=0;
while(i<=slen-wordLen*wordNum&&j<=slen-wordLen)
{
auto it=dict.find(s.substr(j,wordLen));
if(it==dict.end())//this word is not in the dict, clear everything
{
count=0;
curr_table.clear();
i=j+wordLen;
}
else
{
curr_table[it->first]++;
count++;
while(curr_table[it->first]>it->second)
{
string temp=s.substr(i,wordLen);
curr_table[temp]--;
count--;
i+=wordLen;
}
if(count==wordNum)
res.push_back(i);
}
j+=wordLen;
}
curr_table.clear();
}
return res;
}
private:
unordered_map<string,int> dict,curr_table;
};
相关文章推荐
- leetcode 29: Divide Two Integers
- 随手记
- Java泛型详解
- PHP利用SMTP发送邮件,测试ok
- leetcode 28: Implement strStr()
- Android入门(3)——使用TextView实现跑马灯效果
- listview 两个Item可以同时点击
- leetcode 27: Remove Element
- 普通用户不能第一时间升级Win10 Insider用户最先
- Navicat for mysql 远程连接 mySql数据库10061错误问题
- C# 泛型使用笔记
- leetcode 26: Remove Duplicates from Sorted Array
- Ext.bind()使用方法
- leetcode 25: Reverse Nodes in k-Group
- 简单的按钮拖动事件
- UC天气预报接口 测试
- 关于WebView的loaddata方法中文乱码问题解决,记录而已。
- C++ 字典排序 原理与实现
- Spring Java-based容器配置(二)
- leetcode 24: Swap Nodes in Pairs