当前位置:   article > 正文

LeetCode 题解(112): Substring with Concatenation of All Words_you are given , a set of words that are anagrams o

you are given , a set of words that are anagrams of each other. there are no

题目:

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).

题解:

用HashMap简单粗暴比较。唯一的优化是当s中出现重复单词且超过在dict中的数量时,可以将指针向前移动到该重复单词的下一个单词处。

C++版:

  1. class Solution {
  2. public:
  3. vector<int> findSubstring(string s, vector<string>& words) {
  4. int length = words[0].length() * words.size();
  5. vector<int> results;
  6. if(s.length() < length)
  7. return results;
  8. unordered_map<string, int> occurrences;
  9. for(int i = 0; i < words.size(); i++) {
  10. if(occurrences.find(words[i]) == occurrences.end())
  11. occurrences.insert(pair<string, int>(words[i], 1));
  12. else
  13. occurrences[words[i]]++;
  14. }
  15. for(int i = 0; i <= s.length() - length; i++) {
  16. string temp = s.substr(i, length);
  17. test(results, occurrences, temp, (int)words[0].length(), (int)words.size(), i);
  18. }
  19. return results;
  20. }
  21. void test(vector<int>& results, unordered_map<string, int> occurrences, string& s, int l, int n, int& pos) {
  22. for(int i = 0; i < n; i++) {
  23. string current = s.substr(i * l, l);
  24. if(occurrences.find(current) != occurrences.end()) {
  25. if(occurrences[current] > 0)
  26. occurrences[current]--;
  27. else {
  28. pos = pos + s.find(current) + l - 1;
  29. return;
  30. }
  31. } else {
  32. return;
  33. }
  34. }
  35. results.push_back(pos);
  36. }
  37. };

Java版:

  1. public class Solution {
  2. public List<Integer> findSubstring(String s, String[] words) {
  3. List<Integer> results = new ArrayList<Integer>();
  4. if(words.length * words[0].length() > s.length())
  5. return results;
  6. Map<String, Integer> count = new HashMap<>();
  7. for(int i = 0; i < words.length; i++) {
  8. if(count.containsKey(words[i]))
  9. count.put(words[i], count.get(words[i]) + 1);
  10. else
  11. count.put(words[i], 1);
  12. }
  13. for(int i = 0; i <= s.length() - words.length * words[0].length(); i++) {
  14. Map<String, Integer> local = new HashMap<String, Integer>(count);
  15. int j;
  16. for(j = 0; j < words.length; j++) {
  17. String current = s.substring(i + j * words[0].length(), i + (j + 1) * words[0].length());
  18. if(local.containsKey(current)) {
  19. if(local.get(current) > 0)
  20. local.put(current, local.get(current) - 1);
  21. else {
  22. i = s.indexOf(current, i) + words[0].length() - 1;
  23. break;
  24. }
  25. } else {
  26. break;
  27. }
  28. }
  29. if(j == words.length)
  30. results.add(i);
  31. }
  32. return results;
  33. }
  34. }

Python版:

  1. import copy
  2. class Solution:
  3. # @param {string} s
  4. # @param {string[]} words
  5. # @return {integer[]}
  6. def findSubstring(self, s, words):
  7. results = []
  8. if len(s) < len(words) * len(words[0]):
  9. return results
  10. count = {}
  11. for i in words:
  12. if i in count:
  13. count[i] += 1
  14. else:
  15. count[i] = 1
  16. i = 0
  17. while i <= len(s) - len(words) * len(words[0]):
  18. local = copy.copy(count)
  19. j = 0
  20. while j < len(words):
  21. current = s[i + j * len(words[0]) : i + (j + 1) * len(words[0])]
  22. if current in local:
  23. if local[current] > 0:
  24. local[current] -= 1
  25. j += 1
  26. else:
  27. i = s.find(current, i) + len(words[0]) - 1
  28. break
  29. else:
  30. break
  31. if j == len(words):
  32. results.append(i)
  33. i += 1
  34. return results


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/252627
推荐阅读
相关标签
  

闽ICP备14008679号