Using sets


  • 0
    B
    vector<string> findRepeatedDnaSequences(string s) {
        // Result to return
        unordered_set<string> res;
        // Stores unique substrings of size 10
        unordered_set<string> us;
        int n = s.length();
        
        if (n <= 10)
            return {};
            
        for (int i = 0; i <= n-10; i++) {
            string str = s.substr(i, 10);
            // If str is already in the set of substrings, then add it to result set.
            if (us.find(str) != us.end())
                res.insert(str);
            else
                us.insert(str);
        }
        // convert set to vector and return
        return vector<string>(res.begin(), res.end());
    }
    

    };


Log in to reply
 

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