Post

Created by @oliverrichards
 at October 29th 2023, 7:11:31 pm.

Technical Interview Question

Write a function longestCommonPrefix that takes in an array of strings and returns the longest common prefix among them. If there is no common prefix, the function should return an empty string.

For example, given the array ["flower", "flow", "flight"], the function should return "fl" because that is the longest common prefix among these strings. Similarly, given the array ["dog", "racecar", "car"], the function should return an empty string.

Implementation of longestCommonPrefix should be done using string manipulation techniques.

Answer

To solve this problem, we can iterate through the characters of the strings starting from the first character and check if that character is common in all the strings. If it is, we append it to the common prefix. We keep repeating this process for all the characters until we find a character that is not common in all the strings or we reach the end of any string.

Here's the step-by-step detailed explanation of the solution:

  1. First, we check if the input array strs is empty. If it is, there are no strings to compare, so we return an empty string.
  2. We initialize the commonPrefix variable as an empty string. This variable will store the longest common prefix found so far.
  3. We iterate through the characters of the first string in the strs array using a for loop.
  4. Inside the loop, we store the current character in a variable char.
  5. We then use another for loop to iterate through all the strings in the strs array starting from the second string (index 1).
  6. Inside this loop, we check if the current character char is equal to the character at the same index in the current string. If they are not equal, we break the loop and return the commonPrefix as it is, which will be the longest common prefix found so far.
  7. If the current character char is equal to the character at the same index in all the strings, we append it to the commonPrefix string.
  8. After the inner loop finishes, we return to the outer loop and move on to the next character of the first string.
  9. We repeat steps 4-8 until we reach the end of the first string or find a character that is not common in all the strings.
  10. Finally, after both loops complete, we return the commonPrefix string, which will be the longest common prefix among all the strings in the input array.
def longestCommonPrefix(strs):
    if not strs:  # Check if input array is empty
        return ""

    commonPrefix = ""
    for i in range(len(strs[0])):  # Iterate through characters of the first string
        char = strs[0][i]  # Current character of the first string
        for j in range(1, len(strs)):  # Iterate through all other strings
            if i >= len(strs[j]) or strs[j][i] != char:  # Check if character is not common
                return commonPrefix
        commonPrefix += char  # Append common character to commonPrefix
        
    return commonPrefix  # Return commonPrefix if all characters are common

The time complexity of this solution is O(N*M), where N is the length of the array strs and M is the average length of the strings. In the worst case scenario, we have to iterate through all the characters of the longest string in the array.