2018-2 N substring with only one duplicate character

Posted hisonsandiego

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018-2 N substring with only one duplicate character相关的知识,希望对你有一定的参考价值。

 

这是亚麻2018 年新题的第一题:

 

// find all the N substring with only one duplicate character.
#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>

//use a slide window to mared each matched range and record the optimal result.
//use the hash map to index the each substring in target.
//iterate all substring in available container and find out each slide window that is marked with the updated indexs stored in a vector container.

using namespace std;

std::vector<string> subStringKDistance(string& str, int K){
    int s =0;
    int e = K-1;
    unordered_map<char, int> hashmap;
    vector<string> res;
    unordered_set<string> check;
    
    while (e < str.size()){
        hashmap.clear();
        
        //check if there is duplicated char in the slided window by hahtable.
        for ( int p = s; p <= e; p++){
            hashmap[str[p]] += 1;
        }
        
        
        for (auto& e: hashmap){
            if (e.second >= 2){  // have duplicate char.
                goto CONTINUE;
            }
        }
        
        //no duplicate, so check is already existed in the vector using hashtable. if not , insert into the vector.
        //        if ( check.find(str.substr(s,K)) == check.end()){
        //            check.insert(str.substr(s,K));
        //            res.push_back(str.substr(s, K));
        //        }
        //
        //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++;
    }
    
    //this method change the index of each substring, so how to delete the duplicated substring effeciently?
    //    sort(res.begin(),res.end());
    //    unique(res.begin(),res.end());
    
    // how to delete the duplicate substring effeciently?
    // solution : while insert a substring into vector, check a set to see if it is already existed in the set?
    // if yes, that means that susbstring is already in vector, otherwise,it is not.
    
    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 = subStringKDistance(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;
}

 

以上是关于2018-2 N substring with only one duplicate character的主要内容,如果未能解决你的问题,请参考以下文章

#Leetcode# 1016. Binary String With Substrings Representing 1 To N

[LeetCode] Substring with Concatenation of All Words

3. Longest Substring Without Repeating Characters

leetcode 1016. Binary String With Substrings Representing 1 To N

LeetCode Longest Substring with At Most Two Distinct Characters

2017-11 (not fresh grad ) find all substring with N size and only one duplicate character.