solution.md 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
# 串联所有单词的子串
<p>给定一个字符串&nbsp;<strong>s&nbsp;</strong>和一些长度相同的单词&nbsp;<strong>words。</strong>找出 <strong>s
    </strong>中恰好可以由&nbsp;<strong>words </strong>中所有单词串联形成的子串的起始位置。</p>
<p>注意子串要与&nbsp;<strong>words </strong>中的单词完全匹配,中间不能有其他字符,但不需要考虑&nbsp;<strong>words&nbsp;</strong>中单词串联的顺序。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:  s =</strong> &quot;barfoothefoobarman&quot;,<strong>  words = </strong>[&quot;foo&quot;,&quot;bar&quot;]<strong><br />输出:</strong>[0,9]<strong><br />解释:</strong>从索引 0 和 9 开始的子串分别是 &quot;barfoo&quot;&quot;foobar&quot; 。输出的顺序不重要, [9,0] 也是有效答案。</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:  s =</strong> &quot;wordgoodgoodgoodbestword&quot;,<strong>  words = </strong>[&quot;word&quot;,&quot;good&quot;,&quot;best&quot;,&quot;word&quot;]<strong><br />输出:</strong>[]</pre>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
10
<p>以下错误的选项是?</p>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    vector<int> res;
    string s = "barfoothefoobarman";
    vector<string> words{"foo", "bar"};
    res = sol.findSubstring(s, words);

    for (auto i : res)
        cout << i << " ";
    return 0;
}
```

## 答案
```cpp
class Solution
{
public:
    vector<int> findSubstring(string s, vector<string> &words)
    {
        if (words.empty() || s.empty())
            return {};
        vector<int> ans;
        int len = words[0].length(), n = words.size(), total = n * len;
        ;
        int l = s.length();
        unordered_map<string, int> list;
        int i, j, left;
        for (i = 0; i < n; i++)
            list[words[i]]++;
        for (i = 0; i <= l - total; i++)
        {
            unordered_map<string, int> window;
            bool flag = true;
            left = i;
            string str;
            while (left - i < total)
            {
                str = s.substr(left, len);
                if (list.count(str) == 1 && window[str] != list[str])
                {
                    window[str]++;
                    left += len;
                }
                else
                {
                    flag = false;
                }
            }
            if (flag)
                ans.push_back(i);
        }
        return ans;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    vector<int> findSubstring(string s, vector<string> &words)
    {
        vector<int> res;
        if (s.empty() || words.empty())
        {
            return res;
        }
        unordered_map<string, int> ht;
        for (const auto &w : words)
        {
            ht[w]++;
        }
        int len = words[0].length();
        for (int i = 0, j = 0; i < s.length() - words.size() * len + 1; i++)
        {
            unordered_map<string, int> counting;
            for (j = 0; j < words.size(); j++)
            {
                string word = s.substr(i + j * len, len);
                if (++counting[word] > ht[word])
                {
                    break;
                }
            }
            if (j == words.size())
            {
                res.push_back(i);
            }
        }
        return res;
    }
};
```

### B
```cpp
class Solution
{
public:
    vector<int> findSubstring(string s, vector<string> &words)
    {
        if (words.empty() || s.empty())
            return {};
        vector<int> ans;
        unordered_map<string, int> umap1;
        unordered_map<string, int> umap2;
        int count = 0;
        int left = 0;
        for (string str : words)
            ++umap1[str];
        int len = words[0].size();
        int slen = s.size();
        for (int i = 0; i < len; i++)
        {
            left = i;
            count = 0;
            umap2.clear();
            for (int j = i; j <= slen - len; j += len)
            {
                string temp = s.substr(j, len);
                if (umap1.count(temp))
                {
                    umap2[temp]++;
                    count++;
                    while (umap2[temp] > umap1[temp])
                    {
                        string temp2 = s.substr(left, len);
                        --umap2[temp2];
                        --count;
                        left += len;
                    }
                    if (count == words.size())
                    {
                        ans.push_back(left);
                        --umap2[s.substr(left, len)];
                        --count;
                        left += len;
                    }
                }
                else
                {
                    umap2.clear();
                    count = 0;
                    left = j + len;
                }
            }
        }
        return ans;
    }
};
```

### C
```cpp
class Solution
{
public:
    vector<int> findSubstring(string s, vector<string> &words)
    {
        vector<int> result;
        if (words.size() == 0 || s.length() == 0)
            return result;

        map<string, int> words_map;
        int word_len = words[0].length();
        int word_num = words.size();
        if (word_len > s.length())
            return result;
        for (vector<string>::iterator it = words.begin(); it != words.end(); it++)
        {
            if (words_map.count(*it))
                words_map[*it]++;
            else
                words_map[*it] = 1;
        }

        for (int i = 0; i < word_len; i++)
        {
            int left = i, right = i;
            int cur_num = 0;
            map<string, int> s_map;
            string right_str, left_string;
            while (right <= s.length() - word_len)
            {
                right_str = s.substr(right, word_len);

                if (!words_map.count(right_str))
                {
                    right += word_len;
                    left = right;
                    cur_num = 0;
                    s_map.clear();
                }
                else
                {

                    if (!s_map.count(right_str))
                        s_map.insert(pair<string, int>(right_str, 1));
                    else
                        s_map[right_str] += 1;

                    cur_num += 1;
                    right += word_len;

                    if (s_map[right_str] > words_map[right_str])
                    {

                        while (s_map[right_str] > words_map[right_str])
                        {
                            string left_str = s.substr(left, word_len);
                            s_map[left_str]--;
                            cur_num--;
                            left += word_len;
                        }
                    }

                    if (cur_num == word_num)
                    {

                        result.push_back(left);

                        s_map[s.substr(left, word_len)]--;
                        left += word_len;
                        cur_num--;
                    }
                }
            }
        }

        return result;
    }
};
```