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;
}

原文地址:https://www.cnblogs.com/HisonSanDiego/p/8283285.html

时间: 2024-11-25 23:16:28

2018-2 N substring with only one duplicate character的相关文章

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

可能是2018 亚麻的OA题. 1.给字符串, 找出里面长度为N的, 并且里面字符只有一次重复的字串.例子:s: asdfaghjkjqoiiii;N=5.  返回asdfa ghjkj hjkjq jkjqo jqoii. 1 #include <iostream> // std::cout 2 #include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap 3 #incl

2018-2 - target tags

这是亚麻2018 年的新题2 // 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 #includ

Cache Missing

这是亚麻OA 题 // 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 <

[Algorithm] Longest Substring Without Repeating Characters?

Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explana

Longest Substring with At Most K Distinct Characters

Given a string, find the longest substring that contains only two unique characters. For example, given "abcbbbbcccbdddadacb", the longest substring that contains k unique character is "bcbbbbcccb". 分析: 用hashmap记录每个character从start到当前位置

[LeetCode] 340. Longest Substring with At Most K Distinct Characters

Given a string, find the length of the longest substring T that contains at most k distinct characters. Example 1: Input: s = "eceba", k = 2 Output: 3 Explanation: T is "ece" which its length is 3. Example 2: Input: s = "aa",

Pattern类(java JDK源码记录)

1 /* 2 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. 3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 *

Awk by Example--转载

原文地址: http://www.funtoo.org/Awk_by_Example,_Part_1?ref=dzone http://www.funtoo.org/Awk_by_Example,_Part_2 http://www.funtoo.org/Awk_by_Example,_Part_3 In defense of awk In this series of articles, I'm going to turn you into a proficient awk coder. I'

MySQL 常见str函数

MySQL常见的字符串函数 整理自官档. 1.1     SUBSTR or SUBSTRING SUBSTR(str,pos), SUBSTR(str FROM pos),SUBSTR(str,pos,len), SUBSTR(str FROM pos FOR len) SUBSTR() is a synonym for SUBSTRING(). SUBSTRING(str,pos), SUBSTRING(str FROM pos),SUBSTRING(str,pos,len), SUBSTR