LeetCode 14 - Longest Common Prefix - Java Solution and Explanation

preview_player
Показать описание
Here's another LeetCode Java solution from Chaz Winter. This time it's for the Longest Common Prefix problem.
Рекомендации по теме
Комментарии
Автор

8:25 Don't ever do that again. Lol

melindarichardson
Автор

best playlist for beginners, very well explained

ahyar
Автор

Nice job Chaz. Small suggestion. I wouldn't use enhanced for loops here. While they are easier to read, in both the loop where you determine the shortest string, and the inner loop where you compare `y.charAt(i) == test` you are comparing the value at index 0 against itself.

For example for the inner loop I would write it like this:
for (int j = 1; j < strs.length; j++) {
if (strs[j].charAt(i) != test) return shortestString.substring(0, i);
}

It's a micro optimization, but if you make this adjustment to those two loops your solution will beat 100% of solutions. Hopefully that makes sense. Your videos are awesome, I graduated ATA with the Feb 2022 cohort, but I'm going to keep following your videos because I never finished all the leetcode problems myself, and it's fun to learn together.

MrHawkeye