当前位置:   article > 正文

Leetcode30. Substring with Concatenation of All Words_substring with concatenation of all words idea

substring with concatenation of all words idea

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.

Example 1:

Input:
  s = "barfoothefoobarman",
  words = ["foo","bar"]
Output:
[0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

Example 2:

Input:
  s = "wordgoodgoodgoodbestword",
  words = ["word","good","best","word"]
Output:
[]

题目大意:

给定一个字符串 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。

做法稍微复杂了一点,总得来说每次有三层比较,只有三层比较都满足,才算成功。

  1. 第一次比较是比较每一个单词的首字母的个数和所截字符串分成的单词的首字母是否一致,包括种类与个数。
  2. 第二次比较的是所截字符串组里面的所有字母和单词组里面的元素种类个数是否一致。
  3. 第三次也是最后的比较是利用哈希表来存储单词组,再用字符串来匹配,这个比较耗时较多,所以不能直接用,否则会超时,所以将它作为最后的防线。

完整代码如下:

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include<unordered_map>
  5. using namespace std;
  6. bool panduan1(string s,vector<string> &words,int str_len)
  7. {
  8. vector<int> num1(26, 0);
  9. for (int i = 0; i < s.length();i+=str_len)
  10. {
  11. num1[s[i]-'a']++;
  12. }
  13. for (int i = 0; i < words.size();i++)
  14. {
  15. num1[words[i][0] - 'a']--;
  16. if(num1[words[i][0] - 'a']<0)
  17. return false;
  18. }
  19. return true;
  20. }
  21. bool panduan2(string s,vector<string> &words,int n)
  22. {
  23. vector<int> num1(26,0), num2(26,0);
  24. int g1 = words.size();
  25. string s2 = "";
  26. for (int t = 0; t < g1;t++)
  27. {
  28. s2 += words[t];
  29. }
  30. for (int i = 0; i < n; i++)
  31. {
  32. num1[s[i] - 'a']++;
  33. num2[s2[i] - 'a']++;
  34. }
  35. for (int i = 0; i < 26;i++)
  36. {
  37. if(num1[i]!=num2[i])
  38. return false;
  39. }
  40. return true;
  41. }
  42. bool panduan3(string s,vector<string> &words,int str_len)
  43. {
  44. unordered_map<string, int> map;
  45. for (int i = 0; i < words.size();i++)
  46. {
  47. if(map.find(words[i]) == map.end())
  48. map.insert({words[i], 1});
  49. else
  50. map[words[i]]++;
  51. }
  52. for (int i = 0; i < s.length();i+=str_len)
  53. {
  54. string s1 = s.substr(i, str_len);
  55. if(map.find(s1) == map.end())
  56. return false;
  57. else
  58. {
  59. map[s1]--;
  60. if(map[s1]<0)
  61. return false;
  62. }
  63. }
  64. return true;
  65. }
  66. vector<int> findSubstring(string s, vector<string> &words){
  67. vector<int> shuchu;
  68. int len = s.length();
  69. if(len == 0)
  70. return shuchu;
  71. int n = words.size();
  72. if(n == 0)
  73. return shuchu;
  74. int str_len = words[0].length();
  75. int str_len_sum = n * str_len;
  76. for (int q = 0; q <= len-str_len_sum;q++)
  77. {
  78. string s1 = s.substr(q, str_len_sum);
  79. bool t1 = panduan1(s1, words, str_len);
  80. if(t1 == true)
  81. {
  82. bool t2 = panduan2(s1, words, str_len_sum);
  83. if(t2 == false)
  84. t1 = false;
  85. else{
  86. bool t3 = panduan3(s1, words, str_len);
  87. if(t3 == false)
  88. t1 = false;
  89. }
  90. }
  91. if(t1 == false)
  92. continue;
  93. else
  94. shuchu.push_back(q);
  95. }
  96. return shuchu;
  97. }
  98. int main()
  99. {
  100. string s = "abaababbaba";
  101. vector<string> words = {"ab","ba","ab","ba"};
  102. vector<int> num = findSubstring(s, words);
  103. return 0;
  104. }

占用内存较多。可能是申明了很多数组导致的。。。。

     

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

闽ICP备14008679号