-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWords_Concatenation.cpp
More file actions
63 lines (49 loc) · 1.39 KB
/
Words_Concatenation.cpp
File metadata and controls
63 lines (49 loc) · 1.39 KB
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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Words_Concatenation
{
public:
static vector<int> Find_Words_Concatenation(const string &String, const vector<string> &Words)
{
unordered_map<string,int> wordmap;
for(auto str: Words)
{
wordmap[str]++;
}
vector<int> resultindices;
int wordscount=Words.size();
int wordlength=Words[0].length();
for(int i=0;i<=String.length()-wordscount*wordlength;i++)
{
unordered_map<string,int> wordseen;
for(int j=0;j<wordscount;j++)
{
int nextwordindex=i+j*wordlength;
string word=String.substr(nextwordindex,wordlength);
if(wordmap.find(word)==wordmap.end())
{
break;
}
wordseen[word]++;
if(wordseen[word]>wordmap[word])
{
break;
}
if(j+1==wordscount)
{
resultindices.push_back(i);
}
}
}
return resultindices;
}
};
int main(int argc, char *argv[])
{
vector<int> result=Words_Concatenation::Find_Words_Concatenation("catcatcat",vector<string> {"cat","fox"});
for(int i=0;i<result.size();i++)
{
cout<<result[i]<<" ";
}
}