I LOVE This EASY Coding Interview Question! | Length of Last Word - Leetcode 58

preview_player
Показать описание
dynamic programming, leetcode, coding interview question, data structures, data structures and algorithms, faang
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

Wouldn't it be faster since you're already accessing the back of the string to just start counting until you hit a space then return the length of the word

stanleykoh
Автор

That's an awful solution.

Start at the end, go towards the beginning until you find the first letter, the increase the count for each letter you find, and return the count once you find a space

tomalator
Автор

I feel like this channel deliberately gives suboptimal solutions to bait engagement in the comments

JoshBlasy
Автор

Or just start at the index -1 to get the last character and count backwards until you hit a space.. This becomes more efficient the longer your string is.

Dissimulate
Автор

You could also just say: return len(s.split()[-1]) i believe

ElmoSeesYou
Автор

Not a good answer. You can traverse right to left.

haraldschioberg
Автор

I was struggling with my homework, you fixed it!

as
Автор

let str = "Hello my name is Jeff. ";
str = str.trim(); // Remove leading and trailing whitespace
let words = str.split(' '); // Split the string into an array of words
let lastWord = words[words.length - 1]; // Access the last element of the array
console.log(lastWord.length); // Print the last word

XxThatGuyMysteryXx
Автор

why not do the same thing from the back, return when the length is higher than 0 (avoiding space)? (Assuming, a single word is considered as the last word) 🤔

neupanes
Автор

return len(input.strip().split(' ')[-1])

anubis
Автор

idk just split the string, check the last array index if its empty, check the next one until you hit something thats not empty, then check the length of that

bjni
Автор

I fill like you give the best solution so people will go into the comment and try to fix it and then you will have more engagement on the video

AvgBlue
Автор

One line prob same complexity would be to say [st for st s.split(" ") if st][-1]
If u go from the end with the same strat as from front but if u see a space and count is not none return it won't go through the whole str wich helps a lot for huge texts

hunorfekete
Автор

simply invert the string and count characters until seeing the first space:

def lent(s:str):
count = 0
for i in s[::-1]:
if i == " ":
return count
count += 1
return -1

ilm_yanfa
Автор

Why would you go first to last?
Assuming they don't want you to use built in functions (cause if they did it's just _words = sentence.trim().split(' '); return words[words.length - 1].length;_ )

You just start from the end of the string, start counting when you reach a non-space character, and then when you reach a space and the counter is more than 0, that means you just found the length of the last word...

ARKGAMING
Автор

Couldn't you revert the string and count the first word that doesn't have the length 0?

iamtheonlyjohn
Автор

split string by spaces, check for length greater than 0, get last index with length > 0

aaronrockwell
Автор

Why not just return len(arr.split()[-1])

MZGamessuper
Автор

You can also just the strip and split method to do this in a few lines of code

mufasum