use map to solve (c++)


  • 0
    K
    std::vector<string> findRepeatedDnaSequences(string s) {
      std::vector<string> rsl;
      if(s.size() <= 10) {
        return rsl;
      }
    
      int size = s.size(), up = size - 9;
      string tempS;
      map<string, int> m;
      for(int i = 0; i < up; i++) {
        tempS = s.substr(i, 10);
        if(m.find(tempS) != m.end()) {
          if(m[tempS] == 1) {
            rsl.push_back(tempS);
          }
          ++m[tempS];
        } else {
          m[tempS] = 1;
        }
      }
      return rsl;
    }
    

Log in to reply
 

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