Easy C++ Solution (76ms but very easy to understand)


  • 0
    C

    Probably the easiest solution I've seen here to understand. Basically using a unordered_map<string, int> to keep track of how many times the pattern is repeated and then storing it into the vector<string>.

    vector<string> findRepeatedDnaSequences(string s) {
            vector<string> res;
            unordered_map<string, int> mp_substr;
            string findstr;
            if(s.length() <= 10) return res;
            
            for(int i=0; i<s.length()-9; i++) {
                findstr = s.substr(i, 10);
                if(mp_substr.find(findstr) != mp_substr.end()){
                    mp_substr[findstr]++;
                } else {
                    mp_substr[findstr] = 1;
                }
            }
            
            for(auto it: mp_substr) {
                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.