187.
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> ans;
unordered_set<string> dict;
if(s.size()< 10) return ans;
int l =0, r = 9;
while(r< s.size()){
string tmp = s.substr(l, 10);
if(dict.find(tmp)==dict.end()){
dict.insert(tmp);
}else{
vector<string>::iterator it = find(ans.begin(), ans.end(), tmp);
if(it == ans.end()) ans.push_back(tmp);
}
l++;
r++;
}
return ans;
}
};
290.这道题不简单呀
class Solution {
public:
bool wordPattern(string pattern, string s) {
vector<string> v2 = split(s);
unordered_map<char, vector<int>> dict;
for(int i=0; i< pattern.size(); i++){
dict[pattern[i]].push_back(i);
}
for(auto a: dict){
int start = a.second[0];
string str_s = v2[start];
for(auto b: a.second){
if(str_s != v2[b]) return false;
}
}
return true;
}
vector<string> split(string s){
vector<string> ans;
int l=0;
for(int i=0; i<s.size(); i++){
if(s[i]==' ' || s[i]=='\n'){
ans.push_back(s.substr(l, i-l));
l = i+1;
}
}
return ans;
}
};
//错解?怀疑split出错??