Substring with Concatenation of All Words

Subject requirements

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]

Example 2:

Input:
  s = "wordgoodstudentgoodword",
  words = ["word","student"]
Output: []

 

 

Title Analysis

 

This question will give a string and a list of several words. Our task is to find out the subscript of the starting position of all the words in the string and the substring containing only one time.

It is worth noting that all words are the same length. So we can move this length forward every time to extract the substring. In order to make the search and comparison more efficient, I choose to build a map with key as word and value as word frequency.

In order to master the status of the substring being searched in real time, start and end are used to mark the start and end of the substring respectively. When the value of end start is consistent with the total length of the word in the word list, the condition will be triggered to find.

To avoid runtime error s, check that the input string and list are empty.

 

code implementation

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
    	vector <int> result;
    	if (s.empty() || words.empty()) {
    		return result;
		}
        int word_length = words[0].size();
        int total_length = word_length*words.size();
        int start, end;
        map <string, int> dict;
        map <string, int> empty;
        string temp;
        for (auto str:words) {
        	//Determine whether the dictionary contains words
        	if (dict.count(str)) {
        		dict[str]++;
        	}
        	else {
        		dict[str] = 1;
        		empty[str] = 0;
        	}
        }
        map <string, int> ndict = empty;
        for (int i = 0; i < word_length && i + total_length <= s.size(); i++) {
        	start = i;
        	end = i;
        	ndict = empty;
        	while (start + total_length <= s.size()) {
        		temp = s.substr(end, word_length);
        		if (ndict.count(temp)) {
        			if (ndict[temp] < dict[temp]) {
        				ndict[temp]++;
        				end += word_length;
        			}
        			else {
        				ndict = empty;
        				start += word_length;
        				end = start;
        			}
        		}
        		else {
        			end += word_length;
        			start = end;
        			ndict = empty;
        		}
        		if (end-start == total_length) {
        			result.push_back(start);
        			ndict[s.substr(start, word_length)]--;
        			start += word_length;
        		}
        	}
        }
        return result;
    }
};

 

Posted by laide234 on Fri, 27 Dec 2019 07:19:11 -0800