Leetcode Day 4: Longest Common Prefix Explained

The problem is as follows:
Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string “”.

Here is how I solved it:

If there input list of strings is empty, it means th…


This content originally appeared on DEV Community and was authored by Simona Cancian

The problem is as follows:
Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Here is how I solved it:

  • If there input list of strings is empty, it means that there is no common prefix to check so we return an empty string.
if not strs:
    return ""
  • Use the first string in the list as a reference for comparison
for i in range(len(strs[0])):
    char = strs[0][i]
  • For each character in the reference string, compare it with the corresponding character in all other strings. If any string has a different character at the same position or is shorter than current position i, it means that the common prefix ends before this character. Therefore, return the substring of the reference string up to this point.
for string in strs[1:]:
    if i >= len(string) or string[i] != char:
        return strs[0][:i]
  • If loops completes without returning, it means that the reference string itself is the longest common prefix
    return strs[0]

OR just use os standard library available in Python. It includes functions for file and directory manipulation, environment variables, process management etc.
The os module provides a way to interact with the operating system in a portable way.
Here is the completed solution:

import os

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        prefix = os.path.commonprefix(strs)
        return prefix


This content originally appeared on DEV Community and was authored by Simona Cancian


Print Share Comment Cite Upload Translate Updates
APA

Simona Cancian | Sciencx (2024-07-05T05:08:18+00:00) Leetcode Day 4: Longest Common Prefix Explained. Retrieved from https://www.scien.cx/2024/07/05/leetcode-day-4-longest-common-prefix-explained/

MLA
" » Leetcode Day 4: Longest Common Prefix Explained." Simona Cancian | Sciencx - Friday July 5, 2024, https://www.scien.cx/2024/07/05/leetcode-day-4-longest-common-prefix-explained/
HARVARD
Simona Cancian | Sciencx Friday July 5, 2024 » Leetcode Day 4: Longest Common Prefix Explained., viewed ,<https://www.scien.cx/2024/07/05/leetcode-day-4-longest-common-prefix-explained/>
VANCOUVER
Simona Cancian | Sciencx - » Leetcode Day 4: Longest Common Prefix Explained. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/05/leetcode-day-4-longest-common-prefix-explained/
CHICAGO
" » Leetcode Day 4: Longest Common Prefix Explained." Simona Cancian | Sciencx - Accessed . https://www.scien.cx/2024/07/05/leetcode-day-4-longest-common-prefix-explained/
IEEE
" » Leetcode Day 4: Longest Common Prefix Explained." Simona Cancian | Sciencx [Online]. Available: https://www.scien.cx/2024/07/05/leetcode-day-4-longest-common-prefix-explained/. [Accessed: ]
rf:citation
» Leetcode Day 4: Longest Common Prefix Explained | Simona Cancian | Sciencx | https://www.scien.cx/2024/07/05/leetcode-day-4-longest-common-prefix-explained/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.