Why is the empty vector<string> null ?


  • 0
    N

    Your input ""
    Your answer
    Expected answer []

    class Solution {
    public:
        vector<string> findRepeatedDnaSequences(string s) {
            vector<string> res;
            set<string> dnaSet;
            string dna;
        
            for (int i = 0; i < s.size()-10; ++i) {
                dna = s.substr(i, 10);
            
                if (dnaSet.find(dna) != dnaSet.end()) {
                    res.push_back(dna);
                }
                else {
                    dnaSet.insert(dna);
                }
            }
        
            return res;
        }
    };

  • 0
    9

    you need to consider the case n <10
    add

    if(s.size()<10) return res;
    

    also
    modify
    i < s.size()-10 to i<= s.size() -10;


Log in to reply
 

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