Short, Quick & Clean C++ Solution

  • 0

    Store all 10 character length sub string (using the .substr() STL function) in an unordered map as the key and as the value store the frequency of it's occurrence. If the frequency is >1 then return it to the results vector.

    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> res;
        if(s.size()<10) return res;
        unordered_map<string,int> myMap;
        for(int i=0; i<=s.size()-10; ++i) myMap[s.substr(i, 10)]++;
        for(auto it=myMap.begin(); it!=myMap.end(); ++it){
            if(it->second > 1) res.push_back(it->first);
        return res;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.