Easy O(n) C++ sol without bit operations in 12 lines!


  • 1
    A

    class Solution {
    public:
    vector<string> findRepeatedDnaSequences(string s) {

        vector<string> ans;
        unordered_map<string, int> mp;
        
        if(s.length()<10)
        return ans;     
    
        for(int i=0;i<s.length()-9;i++)
        {
            mp[s.substr(i,10)]++;   
        }
        
        for(int i=0;i<s.length()-9;i++)
        {
            //checking for frequency of substring
            if(mp.find(s.substr(i,10))->second>1)
            {
                ans.push_back(s.substr(i,10));
                mp[s.substr(i,10)]=0;
            }
        }
        return ans;    
    }
    

    };


  • -1

Log in to reply
 

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