30. Substring with Concatenation of All Words

1. Description

You are given a string s and an array of strings words. All the strings of words are of the same length.
A concatenated string is a string that exactly contains all the strings of any permutation of words concatenated.

  • For example, if words = [“ab”,“cd”,“ef”], then “abcdef”, “abefcd”, “cdabef”, “cdefab”, “efabcd”, and “efcdab” are all concatenated strings. “acdbef” is not a concatenated string because it is not the concatenation of any permutation of words.

Return an array of the starting indices of all the concatenated substrings in s. You can return the answer in any order.

2. Example

Example 1:
Input: s = “barfoothefoobarman”, words = [“foo”,“bar”]
Output: [0,9]
Explanation:
The substring starting at 0 is “barfoo”. It is the concatenation of [“bar”,“foo”] which is a permutation of words.
The substring starting at 9 is “foobar”. It is the concatenation of [“foo”,“bar”] which is a permutation of words.

Example 2:
Input: s = “wordgoodgoodgoodbestword”, words = [“word”,“good”,“best”,“word”]
Output: []
Explanation:
There is no concatenated substring.

Example 3:
Input: s = “barfoofoobarthefoobarman”, words = [“bar”,“foo”,“the”]
Output: [6,9,12]
Explanation:
The substring starting at 6 is “foobarthe”. It is the concatenation of [“foo”,“bar”,“the”].
The substring starting at 9 is “barthefoo”. It is the concatenation of [“bar”,“the”,“foo”].
The substring starting at 12 is “thefoobar”. It is the concatenation of [“the”,“foo”,“bar”].

3. Constraints

  • 1 <= s.length <= $10^4$
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 30
  • s and words[i] consist of lowercase English letters.

4. Solutions

Sliding Window && Hash Table && Queue

n = s.size(), m = words[0].size(), l = words.size()
Time complexity: O(mn)
Space complexity: O(ml)

class Solution {
public:
    vector<int> findSubstring(const string &s, const vector<string> &words) {
        unordered_map<string, int> required_words;
        for (const string &word : words) {
            ++required_words[word];
        }

        vector<int> match_indexes;
        const int word_count = words.size(), word_len = words[0].size();
        for (int i = 0; i < word_len; ++i) {
            unordered_map<string, int> window = required_words;
            queue<string> words_in_window;
            int left = i, right = i;
            for (int count = word_count; right + word_len <= s.size() && count > 0;
                 right += word_len, --count) {
                string word = s.substr(right, word_len);
                words_in_window.push(word);
                if (--window[word] == 0) {
                    window.erase(word);
                }
            }

            if (window.empty()) {
                match_indexes.push_back(left);
            }

            for (; right + word_len <= s.size(); left += word_len, right += word_len) {
                if (++window[words_in_window.front()] == 0) {
                    window.erase(words_in_window.front());
                }
                words_in_window.pop();

                string right_word = s.substr(right, word_len);
                if (--window[right_word] == 0) {
                    window.erase(right_word);
                }
                words_in_window.push(right_word);

                if (window.empty()) {
                    match_indexes.push_back(left + word_len);
                }
            }
        }

        return match_indexes;
    }
};
comments powered by Disqus