classSolution: deffindRepeatedDnaSequences(self, s: str) -> List[str]: ans = [] cnt = defaultdict(int) for i inrange(len(s) - L + 1): sub = s[i : i + L] cnt[sub] += 1 if cnt[sub] == 2: ans.append(sub) return ans
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { constint L = 10; public: vector<string> findRepeatedDnaSequences(string s){ vector<string> ans; unordered_map<string, int> cnt; int n = s.length(); for (int i = 0; i <= n - L; ++i) { string sub = s.substr(i, L); if (++cnt[sub] == 2) { ans.push_back(sub); } } return ans; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { staticfinalintL=10;
public List<String> findRepeatedDnaSequences(String s) { List<String> ans = newArrayList<String>(); Map<String, Integer> cnt = newHashMap<String, Integer>(); intn= s.length(); for (inti=0; i <= n - L; ++i) { Stringsub= s.substring(i, i + L); cnt.put(sub, cnt.getOrDefault(sub, 0) + 1); if (cnt.get(sub) == 2) { ans.add(sub); } } return ans; } }
public IList<string> FindRepeatedDnaSequences(string s) { IList<string> ans = new List<string>(); Dictionary<string, int> cnt = new Dictionary<string, int>(); int n = s.Length; for (int i = 0; i <= n - L; ++i) { string sub = s.Substring(i, L); if (!cnt.ContainsKey(sub)) { cnt.Add(sub, 1); } else { ++cnt[sub]; } if (cnt[sub] == 2) { ans.Add(sub); } } return ans; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13
const L = 10
funcfindRepeatedDnaSequences(s string) (ans []string) { cnt := map[string]int{} for i := 0; i <= len(s)-L; i++ { sub := s[i : i+L] cnt[sub]++ if cnt[sub] == 2 { ans = append(ans, sub) } } return }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var findRepeatedDnaSequences = function(s) { const L = 10; const ans = []; const cnt = newMap(); const n = s.length; for (let i = 0; i <= n - L; ++i) { const sub = s.slice(i, i + L) cnt.set(sub, (cnt.get(sub) || 0) + 1); if (cnt.get(sub) === 2) { ans.push(sub); } } return ans; };
classSolution: deffindRepeatedDnaSequences(self, s: str) -> List[str]: n = len(s) if n <= L: return [] ans = [] x = 0 for ch in s[:L - 1]: x = (x << 2) | bin[ch] cnt = defaultdict(int) for i inrange(n - L + 1): x = ((x << 2) | bin[s[i + L - 1]]) & ((1 << (L * 2)) - 1) cnt[x] += 1 if cnt[x] == 2: ans.append(s[i : i + L]) return ans
publicclassSolution { constint L = 10; Dictionary<char, int> bin = new Dictionary<char, int> { {'A', 0}, {'C', 1}, {'G', 2}, {'T', 3} };
public IList<string> FindRepeatedDnaSequences(string s) { IList<string> ans = new List<string>(); int n = s.Length; if (n <= L) { return ans; } int x = 0; for (int i = 0; i < L - 1; ++i) { x = (x << 2) | bin[s[i]]; } Dictionary<int, int> cnt = new Dictionary<int, int>(); for (int i = 0; i <= n - L; ++i) { x = ((x << 2) | bin[s[i + L - 1]]) & ((1 << (L * 2)) - 1); if (!cnt.ContainsKey(x)) { cnt.Add(x, 1); } else { ++cnt[x]; } if (cnt[x] == 2) { ans.Add(s.Substring(i, L)); } } return ans; } }
const L = 10 var bin = map[byte]int{'A': 0, 'C': 1, 'G': 2, 'T': 3}
funcfindRepeatedDnaSequences(s string) (ans []string) { n := len(s) if n <= L { return } x := 0 for _, ch := range s[:L-1] { x = x<<2 | bin[byte(ch)] } cnt := map[int]int{} for i := 0; i <= n-L; i++ { x = (x<<2 | bin[s[i+L-1]]) & (1<<(L*2) - 1) cnt[x]++ if cnt[x] == 2 { ans = append(ans, s[i:i+L]) } } return ans }