Substringwith Concatenationof All Words Algorithm
The Substring with Concatenation of All Words Algorithm is a string processing algorithm that aims to find all the starting indices of a given list of words that can be concatenated to form a substring in a given string. The algorithm essentially searches for a continuous sequence of words in the input string that contains each word from the list exactly once, and maintains the order of concatenation. This problem is commonly encountered in various applications such as natural language processing, pattern recognition, and text mining.
The algorithm can be approached using a sliding window technique, where a fixed-size window moves through the input string, and the contents of the window are checked against the given list of words. For each window, a hashmap is used to store the count of words in the current window, and another hashmap is used to store the count of words in the given list. The algorithm then iterates through the input string and checks if the count of words in the current window matches the count of words in the given list. If the counts match, the starting index of the current window is added to the list of valid indices. The window is then moved one position to the right, updating the counts in the hashmap accordingly, and the process repeats until the entire input string has been traversed. This approach allows the algorithm to efficiently search for all possible substrings with concatenation of all words while minimizing the number of comparisons and operations required.
class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) {
vector<int> result;
if (L.empty()) {
return result;
}
int n = L.size();
int len = L[0].size();
map<string, int> hash;
for (int i = 0; i < n; i++) {
hash[L[i]] += 1;
}
for (int i = 0; i < len; i++) {
vector<string> slices;
for (int j = i; j + len <= S.size(); j += len) {
slices.push_back(S.substr(j, len));
}
for (int j = 0; j + n <= slices.size(); j++) {
map<string, int> found;
for (int k = 0; k < n; k++) {
found[slices[j+k]] += 1;
if (found[slices[j+k]] > hash[slices[j+k]]) {
break;
}
if (k == n - 1) {
result.push_back(i + j * len);
}
}
}
}
return result;
}
};