bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 14. Longest Common Prefix

14. Longest Common Prefix

Leetcode Solutions

Approach: Horizontal scanning

  1. Initialize the prefix as the first string in the array.
  2. Iterate over the array of strings starting from the second string.
  3. For each string, compare it with the current prefix and update the prefix to the common part.
  4. If at any point the prefix becomes an empty string, break out of the loop as there is no common prefix.
  5. Return the prefix after iterating through all strings.
UML Thumbnail

Approach: Vertical scanning

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...