filmov
tv
LeetCode 14: Longest Common Prefix - Interview Prep Ep 19
Показать описание
LeetCode 14. Longest Common Prefix
Solution explained:
Solution 1: Horizontal scan
1. we can scan every single word in this given list, we assume the first word is the longest common prefix,
3. after we go through all words in the list, we can just return the final prefix string as the answer.
Solution 2: Vertical scan
1. we scan every single character of every single word by beginning from the first character of the first word, we'll use the first word as the possible longest common prefix candidate to start with;
2. in the inner for loop, we first check if the current character index i is equal to the current word length, if that's the case, we could simply break out of the loop and return the current prefix candidate as the result, or if the current word's character is not equal to the one that's at the same index from the first word, we should also break out and return;
3. if we could finish the two nested for loops until the end, that means, the very first word is our longest common prefix.
⭐ Support my channel and connect with me:
// TOOLS THAT I USE:
// MY FAVORITE BOOKS:
Coding interview made simple!
My ENTIRE Programming Equipment and Computer Science Bookshelf:
And make sure you subscribe to my channel!
Your comments/thoughts/questions/advice will be greatly appreciated!
#softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures
Комментарии