8-line AC C++ solution


  • 0

    Didn't use those popular bit manipulation to translate string into code for the first time, but got AC.
    Learnt a lot from other's posts about the disadvantage of using direct string as key. But this solution is just straightforward enough, push to result only when it shows for the second time to avoid dupes.

    class Solution {
    public:
        vector<string> findRepeatedDnaSequences(string s) {
            unordered_map<string, int> mp;
            vector<string> res;
            if(s.size() < 10) return res;
            
            for(int i = 0; i <= s.size() - 10; i++){
                string ss = s.substr(i, 10);
                if(mp[ss] == 1) res.push_back(ss);
                mp[ss]++;
            }
            
            return res;        
        }
    };

  • 0
    1

    class Solution {
    public:
    vector<string> findRepeatedDnaSequences(string s) {
    unordered_map<string, int> mp;
    vector<string> res;
    if(s.size() < 10) return res;

        for(int i = 0; i <= s.size() - 10; i++){
            string ss = s.substr(i, 10);
            if(mp[ss] == 1) res.push_back(ss);
            mp[ss]++;
        }
    
        return res;        
    }
    

    };


Log in to reply
 

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