DEV Community

Discussion on: LeetCode WalkThru: 'First Unique Character in a String'

Collapse
 
shock451 profile image
Abdullahi Ayobami Oladosu • Edited

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++) {
...