Nice post! However, there are some issues with the snippet of code.
It does not pass some of the tests you provided.
Did you mean to return -1 after the loop rather than within the else block?
I would also like to add that you could iterate over unique letters rather than the full string to avoid repeating computations since the time complexity of indexOf is O(n) in the worst case.
let s = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef";
const uniqueLetters = [...new Set(s)]; // "ef"
for (let i = 0; i < uniqueLetters.length; i++) {
...
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice post! However, there are some issues with the snippet of code.
It does not pass some of the tests you provided.
Did you mean to
return -1after the loop rather than within the else block?I would also like to add that you could iterate over unique letters rather than the full string to avoid repeating computations since the time complexity of indexOf is O(n) in the worst case.
let s = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef";
const uniqueLetters = [...new Set(s)]; // "ef"
for (let i = 0; i < uniqueLetters.length; i++) {
...