亚麻:2018-1 find K distinct substring without duplication
这是亚麻2018 年新题的第一题:
// find K distinct substring whithout duplication
#include <iostream> // std::cout
#include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector> // std::vector
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <sys/time.h>
//solution:
//data structure: slide window and hashmap.
//algorithm:use slide window<sp , ep> to mark each substring and check if the substring have duplicted character.
//use the hash map<char , int > check if the substring have duplicated character. if no, and it is not in res, then load it into res.
//iterate all substring. so the comlexity is c(n).
using namespace std;
std::vector<string> subStringKDistinct(string& str, int K){
int s =0;
int e = K;
unordered_map<char, int> hashmap; // use to check if the susbtring have duplicated character.
vector<string> res; // load the findings.
while (e <= str.size()){
hashmap.clear();
//check if there is duplicated char in the slided window by hashtable.
for ( int p = s; p < e; p++){
hashmap[str[p]] += 1;
}
for (auto& e: hashmap){
if (e.second >= 2){ // check duplicated character. if having duplicate char. do not meet the requirement.
goto CONTINUE;
}
}
//check if duplicate using == of two substring. comparing with the hashtable, the effeciency is almost same.
for (auto& e: res){
if (e == str.substr(s,K))
goto CONTINUE;
}
res.push_back(str.substr(s, K));
CONTINUE: //move the slide window.
s++;
e++;
}
return res;
}
int main () {
struct timeval tv;
gettimeofday(&tv,NULL);
long ts = tv.tv_sec * 1000 + tv.tv_usec / 1000;
string s ={"awaglknagawunagwkwagl"};
cout<<"length of the string: " << s.size() << endl;
vector<string>&& out = subStringKDistinct(s, 4);
for (auto& e : out){
cout<< e << endl;
}
gettimeofday(&tv,NULL);
long te = tv.tv_sec * 1000 + tv.tv_usec / 1000;
cout<< "running tmie is : " << te - ts << endl;
return 0;
}