[Leetcode] 187 Repeated DNA Sequences, leetcoderepeated
(1) The first practice is to use map <string, int> to record the number of strings with 10 characters. If it exceeds 2, push_back is entered into ans. But MLE indicates that using string is not a good method.
Below is the MLE code:
class Solution {public: vector<string> findRepeatedDnaSequences(string s) { vector <string> ans; map<string,int> mp; if(s.length()<10) return ans; for(int i=0;i<s.length()-10;i++) mp[s.substr(i,10)]++; map<string,int>::iterator it; for(it=mp.begin();it!=mp.end();++it) { if(it->second>1) ans.push_back(it->first); } return ans; }};
(2) After reading the Tags, I am prompted to use the bitwise operation. This reminds me of the uniqueness of the prefix code of the Hoffmann code, so the following mark can be used here:
A: 00 T: 01 C: 10G: 11 A total of 10 characters, A total of 20 characters, and an int has 32 characters, so map <int, int> processing can reduce space usage.
The clock maintains a 20-bit space. During traversal, the first character is removed from the left 14-bit space, and the first character is removed from the right 12-bit space, in this way, it has played a role in saving time.
class Solution {public: vector<string> findRepeatedDnaSequences(string s) { vector <string> ans; map <int,int> mp; map <char,int> cur; set<string> st; cur['A']=0; cur['T']=1; cur['C']=2; cur['G']=3; if(s.length()<10) return ans; int temp; for(int i=0;i<9;i++) { temp<<=2; temp|=cur[s[i]]; } // mp[temp]++; for(int i=9;i<s.length();i++) { temp<<=14; temp>>=12; temp|=cur[s[i]]; mp[temp]++; if(mp[temp]>=2) st.insert(s.substr(i-9,10)); } set<string>::iterator it; for(it=st.begin();it!=st.end();it++) ans.push_back(*it); return ans; }};
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.