Substring with Concatenation of All Words
来源:互联网 发布:宜搜小说软件下载 编辑:程序博客网 时间:2024/06/08 16:04
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**: "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) { vector<int> result; if(s.empty() || words.empty() || words.size() * words[0].size() > s.size()) return result; unordered_map<string, int> target_map; for(auto &word : words) ++target_map[word]; int num_words = words.size(); int char_word = words[0].size(); //遍历源字符串查找而不是目标数组 for(int index = 0; index <= s.size() - num_words * char_word; index++ ) { unordered_map<string, int>target_exit; int j; for(j = 0; j < num_words; j++) { string target = s.substr(index + j * num_words, num_words); if(target_map.find(target) == target_map.end()) break; ++target_exit[target]; if(target_map[target] < target_exit[target]) break; } if(j == num_words) { cout << "tset" << endl; result.push_back(index); } } return result; } };
官方给出的O(n)算法
vector<int> findSubstring(string S, vector<string> &L) { vector<int> ans; int n = S.size(), cnt = L.size(); if (n <= 0 || cnt <= 0) return ans; // init word occurence unordered_map<string, int> dict; for (int i = 0; i < cnt; ++i) dict[L[i]]++; // travel all sub string combinations int wl = L[0].size(); for (int i = 0; i < wl; ++i) { int left = i, count = 0; unordered_map<string, int> tdict; for (int j = i; j <= n - wl; j += wl) { string str = S.substr(j, wl); // a valid word, accumulate results if (dict.count(str)) { tdict[str]++; if (tdict[str] <= dict[str]) count++; else { // a more word, advance the window left side possiablly while (tdict[str] > dict[str]) { string str1 = S.substr(left, wl); tdict[str1]--; if (tdict[str1] < dict[str1]) count--; left += wl; } } // come to a result if (count == cnt) { ans.push_back(left); // advance one word tdict[S.substr(left, wl)]--; count--; left += wl; } } // not a valid word, reset all vars else { tdict.clear(); count = 0; left = j + wl; } } } return ans; }
0 0
- LeetCode: Substring with Concatenation of All Words
- LeetCode : Substring with Concatenation of All Words
- LeetCode29: Substring with Concatenation of All Words
- [Leetcode] Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- [LeetCode]Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- LeetCode-Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- 26 - Substring with Concatenation of All Words
- LeetCode:Substring with Concatenation of All Words
- Leetcode: Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- LeetCode Substring with Concatenation of All Words
- Android string.xml字符串的格式化和样式
- Markdown的常用语法
- 一组数据中只有一个数字出现了一次。 其他所有数字都是成对出现的。请找出这个数字。(使用位运算)
- ssm的文件上传
- layer回调函数
- Substring with Concatenation of All Words
- C#完成超酷的图像效果 (附demo)
- 算法/排序算法/QuickSort快速排序
- C++常用库(转)
- Android Studio依赖项目出现unspecified on project app resolves to an APK archive which is not supported
- 如何将安卓虚拟机中的数据库文件导出到本地
- 常用tf函数汇总
- easyUI tabs控件显示与隐藏以及跳转
- extjs4.2加背景图片