[LeetCode] Longest Common Prefix (Java)

Write a function to find the longest common prefix string amongst an array of strings.


This is a simple problem. We can just check for each position of every string in the string array. But we need to take care of some strings that is shorter than others.

Since there is two loops, we can break the outer loop if we found a mismatch or we have reached the end of certain string.



In the best case we need to check every character of each string. Assuming the average length of string is m, and there are n strings. Then the complexity is O(mn).