[Leetcode 30, Hard] Substring with Concatenation of All Words
来源:互联网 发布:linux设置ip地址命令 编辑:程序博客网 时间:2024/05/01 20:51
Problem:
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 wordsexactly 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).
Analysis:
Solutions:
C++:
vector<int> findSubstring(string s, vector<string>& words) { map<string, int> candidates; for(int i = 0; i < words.size(); ++i) { if(candidates.find(words[i]) == candidates.end()) candidates[words[i]] = 1; else ++candidates[words[i]]; } int num_test_words = words.size(); int word_len = words[0].size(); vector<int> results; for(int start = 0; start < (int)s.size() - num_test_words * word_len + 1; ++start) { bool is_valid = true; map<string, int> local_count; int end = start + num_test_words * words[0].size() - 1; for(int j = start; j <= end;) { if(candidates.find(s.substr(j, word_len)) == candidates.end()) { is_valid = false; break; } else if(local_count.find(s.substr(j, word_len)) != local_count.end() && candidates[s.substr(j, word_len)] == local_count[s.substr(j, word_len)]) { is_valid = false; break; } else { if(local_count.find(s.substr(j, word_len)) == local_count.end()) local_count[s.substr(j, word_len)] = 1; else ++local_count[s.substr(j, word_len)]; j += word_len; } } if(is_valid) results.push_back(start); } return results; }Java:
Python:
0 0
- [Leetcode 30, Hard] Substring with Concatenation of All Words
- Leetcode 30. Substring with Concatenation of All Words (Hard) (cpp)
- 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
- [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
- 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
- Leetcode: Substring with Concatenation of All Words
- 【分享】School Rumble校园迷糊大王PSP姐姐事件+PS2游戏第一,二学期【带VNR翻译教程】
- 我的奋斗1
- sql的分组统计与group by 日期的处理
- Ubuntu 更新内核及删除无用文件
- Linux shell命令
- [Leetcode 30, Hard] Substring with Concatenation of All Words
- poj 3009 Curling2.0 回溯搜索
- 你了解实时计算吗?
- pipelined function
- [Leetcode 10, Hard] Regular Expression Matching
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest
- warning: strtotime(): It is not safe to rely on the system's timezone settings
- 将Latex tex文档转换成 word文档(下)
- 1212