Find the Longest Common Prefix - JavaScript

preview_player
Показать описание
Elegant solution to finding the longest common prefix question on leetcode.

#softwareengineering #javascript #shorts

Music
Creative Commons / Attribution 3.0 Unported License (CC BY 3.0)
Рекомендации по теме
Комментарии
Автор

I recommend adding maxCheckLength = min(str, str.length)
Then change loop conditions to i< maxCheckLength

hikari
Автор

You could also find the longest word in the array and count up to the characters in that to avoid having to do that nested loop.

MaJetiGizzle
Автор

No need to slice just compare each index for each word so index 0 for each word than index 1 etc if theres a difference return the last succesful result

lostgoat
Автор

We can use Trie structure but I think we can keep it simple for this

armandomendivil
Автор

Isn't this a candidate for Tries?

denvergomes
Автор

The base string should be the shortest in the strs list, otherwise you might get "index out of bounds".

tellacheimad
Автор

Iterate frew the flower? What is that?

supremoluminary
Автор

What website is that? That has all the problems to solve. I can't tell from the logo.

masomthefoodie
Автор

i would find the longest string in the array first, then continue with the method in the video

salamiswami
Автор

Wrong. This solution would fail the interview.

Optimal is to sort the array and compare index 0 and -1.

mapledanish