博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Substring with Concatenation of All Words
阅读量:4150 次
发布时间:2019-05-25

本文共 2100 字,大约阅读时间需要 7 分钟。

class Solution {//because all the word have same length, if they do not have the same length then they can not be solve by //the solution below//so it can be solve by iterating finding the substring in the input stringpublic:	vector
findSubstring(string S, vector
&L) { map
words;//record words, to find if the substring is in the words map
curStr;//keep record of the occurrence time of every word for(int i = 0; i < L.size(); ++i) ++words[L.at(i)]; int N = L.size(); vector
ret; if(N <= 0) return ret; int M = L.at(0).size(); for(int i = 0; i <= (int)S.size()-N*M; ++i) { curStr.clear(); int j = 0; for(j = 0; j < N; ++j) { string w = S.substr(i+j*M, M); if(words.find(w) == words.end()) break; ++curStr[w]; if(curStr[w] > words[w]) break; } if(j == N) ret.push_back(i); } return ret; }};

second time

class Solution {public:    vector
findSubstring(string S, vector
&L) { // Start typing your C/C++ solution below // DO NOT write int main() function if(L.size() == 0) return vector
(); int wordLen = L[0].size(); int wordNum = L.size(); int wordsLen = wordLen*wordNum; vector
ans; map
wordMap; for(int i = 0; i < L.size(); ++i) { if(wordMap.find(L[i]) == wordMap.end()) wordMap[L[i]] = 1; else wordMap[L[i]]++; } for(int i = 0; i < S.size(); ++i) { int j = wordsLen-1+i; if(j < S.size()) { map
curWordMap; bool flag = true; for(int k = i, curWordNum = 0; curWordNum < wordNum; k += wordLen, curWordNum++) { string tmpStr = S.substr(k, wordLen); if(curWordMap.find(tmpStr) == curWordMap.end()) curWordMap[tmpStr] = 1; else curWordMap[tmpStr]++; if(wordMap.find(tmpStr) == wordMap.end() || curWordMap[tmpStr] > wordMap[tmpStr]) { flag = false; break; } } if(flag) ans.push_back(i); } } return ans; }};

转载地址:http://rqxti.baihongyu.com/

你可能感兴趣的文章
引用还是指针?
查看>>
checkio-non unique elements
查看>>
checkio-medium
查看>>
checkio-house password
查看>>
checkio-moore neighbourhood
查看>>
checkio-the most wanted letter
查看>>
Redis可视化工具
查看>>
大牛手把手带你!2021新一波程序员跳槽季,全套教学资料
查看>>
JAVA自定义注解与通过反射去解析注解参数
查看>>
Effective Java学习(创建和销毁对象)之——通过私有化构造器强化不可实例化的能力...
查看>>
Effective Java学习(创建和销毁对象)之——消除过期对象引用
查看>>
Effective Java学习(泛型)之——消除非受检警告
查看>>
Effective Java学习(泛型)之——List列表优先于数组
查看>>
Effective Java学习(泛型)之——优先使用泛型化
查看>>
Effective Java学习(泛型)之——优先考虑泛型化方法
查看>>
Effective Java学习(方法)之——返回零长度的数组或者集合,而不是null
查看>>
Effective Java学习(通用程序设计)之——将局部变量的作用最小化
查看>>
Effective Java学习(通用程序设计)之——for-each循环优先于传统的for循环
查看>>
Effective Java学习(通用程序设计)之——如果需要精确的答案,请避免使用float和double...
查看>>
Effective Java学习(通用程序设计)之——如果其他类型更适合,请尽量避免使用字符串...
查看>>