LeetCode 14. Longest Common Prefix || Java Solution Walkthrough

preview_player
Показать описание
LeetCode is a great platform for people who want general coding practice, whether that be for interviews or just wanting to practice their problem solving. I will be taking you through all the steps of finding the optimal solution to each problem on the site following a logical ordering setout by LeetCode themselves.

In this video, we are going over the problem 14. Longest Common Prefix and the goal is to loop through the array of strings, and determine which part of every string matches inside that array and return it.

To solve this problem there are once again a couple of options, we could cheat again using the indexOf method from the String class, but again this doesn't seem very fun. So instead, we will create our own method. The first thing we can think about is what is the longest common prefix and are there any rules/knowelege we can use. First thing that comes to mind is that the longest common prefix is always included inside of each string, so we first must find the small string in the list, then once we have the smallest string we can iterate through that smallest string and all the other string to see how many characters match from that smallest, if none match we return an empty string, otherwise continue looping until we don't match on all the strings anymore.

~~~ Stay Up To Date With My Social Media ~~~

#unknownkoder #leetcode #algorithms
Рекомендации по теме