-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_30.cpp
84 lines (72 loc) · 2.76 KB
/
Solution_30.cpp
1
2
3
4
5
6
7
8
9
10
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
//
// Created by houmo on 3/22/19.
//
/*
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: []
*/
#include "Solution_30.h"
vector<int> Solution_30::findSubstring(string s, vector <string> &words) {
vector<int> Answer;
if(words.size()==0 || s.size()==0 || s.size()<words[0].size()){
return Answer;
}else{
int lengthOfAWord = words[0].length();
int sizeOfvec = words.size();
vector<int> FoundGoal{1};
vector<string> wordsTemp{words[0]};
for(int i=1;i<words.size();i++){
if(find(wordsTemp.begin(), wordsTemp.end(),words[i])!=wordsTemp.end()){
FoundGoal[find(wordsTemp.begin(), wordsTemp.end(),words[i])-wordsTemp.begin()]++;
}else{
wordsTemp.push_back(words[i]);
FoundGoal.push_back(1);
}
}
vector<int> Found(FoundGoal.size(),0);
int inFinding = 0;
for(int i=0; i<=s.size()-lengthOfAWord; i++){
vector<string>::iterator Index = find(wordsTemp.begin(),wordsTemp.end(),s.substr(i,lengthOfAWord));
if(Index != wordsTemp.end()){
inFinding = 1;
Found[Index-wordsTemp.begin()] ++;
for(int j=1; j<sizeOfvec;j++){
Index = find(wordsTemp.begin(),wordsTemp.end(),s.substr(i+j*lengthOfAWord,lengthOfAWord));
if(Index == wordsTemp.end()){
inFinding = 0;
for(int k=0; k<Found.size(); k++){
Found[k] = 0;
}
break;
}
Found[Index-wordsTemp.begin()] ++;
if(Found[Index-wordsTemp.begin()] > FoundGoal[Index-wordsTemp.begin()]){
inFinding = 0;
for(int k=0; k<Found.size(); k++){
Found[k] = 0;
}
break;
}
}
if(inFinding == 1){
Answer.push_back(i);
for(int k=0; k<Found.size(); k++){
Found[k] = 0;
}
}
}
}
return Answer;
}
}