标签:algorithm leetcode 面试题 substring 算法
vector<int> findSubstring(string S, const vector<string> &L) { vector<int> re; if(S.size() == 0 || L.size() == 0) return re; unordered_map<string, int> total; unordered_map<string, int> has; int wordNum = L.size(); int wordLen = L[0].size(); int searchEnd = S.size() - wordLen * wordNum; for (int i = 0; i < wordNum; ++i) { total[L[i]]++; } for (int i = 0; i <= searchEnd; ++i) { int j = i; has.clear(); int iword = 0; for (; iword < wordNum; ++iword) { string sub = S.substr(j, wordLen); //not in L if(total[sub] == 0) break; //in L, but redundancy if(++has[sub] > total[sub]) break; j += wordLen; } if(iword == wordNum) { re.push_back(i); } } return re; }
【leetcode】Substring with Concatenation of All Words,布布扣,bubuko.com
【leetcode】Substring with Concatenation of All Words
标签:algorithm leetcode 面试题 substring 算法
原文地址:http://blog.csdn.net/shiquxinkong/article/details/27806183