Solving LeetCode 387 in JavaScript (First Unique Character in a String)

preview_player
Показать описание
In this video I solve LeetCode problem 387 (First Unique Character in a String) with the JavaScript programming language. This question has previously appeared during Amazon coding interviews.

I start by helping you understand what the problem is asking for, then I will go over the pseudo-code/logic for a solution, and finally I will write the solution code explaining each line, step-by-step.

Using my simple and easy-to-understand methodology, I make difficult and complex interview questions *incredibly* easy to understand. So easy in fact, that even your grandma could do it! 👵🏻

I will be solving a new problem EVERY WEEK, so be sure to smash that subscribe button and hit the notification bell so you don't miss a single one! 😀

-----
I also offer full-length interview preparation courses, programming tutorials, and even 1-1 tutoring!

Who knew acing your next software engineering interview could be *so* incredibly easy!

-----
#programming #codinginterview #leetcode
Рекомендации по теме
Комментарии
Автор

Hi! I'd love to see 1-3 examples of ways to solve it and explanations of why and how the run times differ. Thanks for making these!

jiggabytes
Автор

A really efficient way of solving this problem is with indexOf and lastIndexof methods. If the index fo the current element at i matches with the last occurrence of that index then we know its a unique character.

for (let i =0; i < str.length ; i++){
if( str.indexOf( str[i] ) === str.lastIndexOf( str[i] )){
return i
}
}
return -1

However learning how to solve such a problem with a Map is more beneficial in the long term due to its wider applicability.

Ms
Автор

3:56 Great video! But I do have one question. Why is the Time complexity O(n) if we are looping through it twice? Why is it not O(N * 2)?

JamieDawsonCodes