Загрузка...

Doing a coding challenge - live stream - First Unique Character in a String!

Join me to work through a coding challenge live from Leetcode.com called First Unique Character in a String!

Problem:
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

Solution summary:
— Intuition / tip: Use an object/map to store the frequency of each character first
— Create an object/map to hold frequencies
— Loop the input array - for each item either add it to the map with a value of 1, or increment the value if already in the map
— Loop the values of the string
— For each item check if the frequency in the map equals to 1, if so, return that index (that is the index of the first unique character). If not, keep looping.
— Return -1 at the end the function (there is no unique character if we get here)
— Time complexity: O(n) where ’n’ is the number of characters of the input string
— Space complexity: O(n) where ’n’ is the number of characters of the input string

Let me know if you have any questions or see ways I could have optimized!

Bonus: at the end of the video I answer questions.

Let's connect!
🌐 Website | https://www.kg.codes
📺 Subscribe | www.youtube.com/channel/UCfv7SNcMWwn8QANjD7AgY5w?sub_confirmation=1
📷 Instagram | www.instagram.com/kg.codes/
🎵 TikTok | https://www.tiktok.com/t/ZTRLxP4wf/
🐦 Twitter | www.twitter.com/kgcodes/

Get my coding gear / desk setup 👨🏾‍💻
📦 Amazon | https://www.amazon.com/shop/kg.codes/list/3E7C7N52AFY8R

Видео Doing a coding challenge - live stream - First Unique Character in a String! канала KG.codes
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять