Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions javascript/0187-repeated-dna-sequences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* https://leetcode.com/problems/repeated-dna-sequences/
* Hashing
* s = the number of letters in the sequance. In our case it's 10. so the time complexity would be 10*n which boils down to n.
* Time O(s*n) | Space O(n)
* @param {string} s
* @return {string[]}
*/

var findRepeatedDnaSequences = function(s) {

const sequanceStack = new Set();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable names are misleading

const sequenceSet = new Set();
const resultSet = new Set();

return [ ...resultSet ];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, my bad. Sorry. I updated the variables.

let resultStack = new Set();

for(let i = 0; i < s.length; i++) {
const subSequance = getSubSequance(s,i,10);
if(sequanceStack.has(subSequance)) {
resultStack.add(subSequance);
} else {
sequanceStack.add(subSequance);
}
}

resultStack = [...resultStack];
return resultStack;
};

function getSubSequance(s,i,len) {
return s.slice(i, i + len);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: This will change your time complexity. Refactor or review your Time O()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, the len parameter is a constent(10 in this case). so the the time complexity would be 10*n which is still n right? Aren't we suppose to ignore the coefficient? But I have changed the time coplexity in the comment nonetheless.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! Since it is a constant, we can ignore the auxiliary space.

}