Created
May 24, 2017 04:22
-
-
Save jpoechill/3990140cfbb5800c4d22141f1abafa65 to your computer and use it in GitHub Desktop.
repeatedDNASequences via. CodeFights
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T. In research, it can be useful to identify repeated sequences within DNA. | |
Write a function to find all the 10-letter sequences (substrings) that occur more than once in a DNA molecule s, and return them in lexicographical order. These sequences can overlap. | |
Example | |
For s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", the output should be | |
repeatedDNASequences(s) = ["AAAAACCCCC", "CCCCCAAAAA"]. | |
Input/Output | |
[time limit] 4000ms (js) | |
[input] string s | |
Guaranteed constraints: | |
0 ≤ s.length ≤ 5000. | |
[output] array.string | |
An array containing all of the potential 10-letter sequences that occur more than once in s. | |
function repeatedDNASequences(s) { | |
// All 10 letter sequences | |
var myHash = {} | |
var repeatedSequences = [] | |
for (var i = 0; i < (s.length - 9); i ++) { | |
var thisSubString = s.slice(i, i + 10) | |
if (!myHash.hasOwnProperty(thisSubString)) { | |
myHash[thisSubString] = 1 | |
} else if (!repeatedSequences.includes(thisSubString)) { | |
// Didn't actually need this line below | |
// myHash[thisSubString]++ | |
repeatedSequences.push(thisSubString) | |
} | |
} | |
console.log(repeatedSequences) | |
return repeatedSequences.sort() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment