Share my C++ solution,easy to understand


  • 0
    V
    class Solution {
    public:
        vector<string> findRepeatedDnaSequences(string s) {
            int i = 0, n = s.size();
            string temp;
            unordered_map<string, int> m;
            vector<string> ret;
            
            if (n < 11)
                return ret;
            
            n -= 10;
            for (i = 0; i <= n; ++i)
            {
                temp = s.substr(i, 10);
                
                if (m.find(temp) != m.end())
                {
                    if (m[temp] == 1)
                    {
                        ret.push_back(temp);
                        m[temp]++;
                    }
                }
                else
                    m[temp] = 1;
            }
            
            return ret;
        }
    };

Log in to reply
 

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