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

  • 0

    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()){
                } else {
                    mp_substr[findstr] = 1;
            for(auto it: mp_substr) {
                if(it.second > 1)
            return res;

Log in to reply

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