|
| 1 | +""" |
| 2 | +Write a function to find the longest common prefix string amongst an array of strings. |
| 3 | +
|
| 4 | +If there is no common prefix, return an empty string "". |
| 5 | +
|
| 6 | +
|
| 7 | +Example 1: |
| 8 | +
|
| 9 | + Input: strs = ["flower","flow","flight"] |
| 10 | + Output: "fl" |
| 11 | +
|
| 12 | +Example 2: |
| 13 | +
|
| 14 | + Input: strs = ["dog","racecar","car"] |
| 15 | + Output: "" |
| 16 | + Explanation: There is no common prefix among the input strings. |
| 17 | +
|
| 18 | +Constraints: |
| 19 | +
|
| 20 | + 1 <= strs.length <= 200 |
| 21 | + 0 <= strs[i].length <= 200 |
| 22 | + strs[i] consists of only lowercase English letters if it is non-empty. |
| 23 | +
|
| 24 | +""" |
| 25 | + |
| 26 | +from typing import List |
| 27 | + |
| 28 | +def longestCommonPrefix(strs: List[str]) -> str: # noqa |
| 29 | + """ Function to find the longest prefix. |
| 30 | +
|
| 31 | + Notes: |
| 32 | + 1. Complexity is O(N) in average. The worst case might be O(N^2)! |
| 33 | + 2. limitations has been defined on the top of the function. |
| 34 | + 3. Valid only for strs which content str size are not larger than 200! |
| 35 | +
|
| 36 | + Returns: |
| 37 | + str of the longest prefix over all possible strs. |
| 38 | + """ |
| 39 | + min_len: int = 200 # declare maximum which is expected |
| 40 | + min_str: str = "" # declare default output if None strs. Here as well we will save result. |
| 41 | + |
| 42 | + if not strs: |
| 43 | + return min_str |
| 44 | + |
| 45 | + # Find minimum str in the strs |
| 46 | + for txt in strs: |
| 47 | + current_len = len(txt) |
| 48 | + if current_len <= min_len: |
| 49 | + min_str = txt |
| 50 | + min_len = current_len |
| 51 | + |
| 52 | + # Check that all str in strs have the same prefix. |
| 53 | + for txt in strs: |
| 54 | + # In case we have reached min_len limitation we break the loop earlier! |
| 55 | + if min_len <= 0: |
| 56 | + break |
| 57 | + |
| 58 | + # Current prefix for str depends on min_len |
| 59 | + tmp_str = txt[:min_len] |
| 60 | + |
| 61 | + if tmp_str != min_str: |
| 62 | + # If prefix for selected str are not the same for tmp str prefix we create loop and init a counter which is equal to min_len. |
| 63 | + counter = min_len |
| 64 | + while counter >= 0: |
| 65 | + # Check what prefix size are equal for tmp str prefix. |
| 66 | + condition = min_str[:counter] == txt[:counter] |
| 67 | + if condition: |
| 68 | + # Update min_len and min_str when prefixes are valid for each str. |
| 69 | + min_str = min_str[:counter] |
| 70 | + min_len = counter |
| 71 | + # break the loop because the condition success! |
| 72 | + break |
| 73 | + # If checker for selected min_len failed than we update the counter. |
| 74 | + counter -= 1 |
| 75 | + return min_str |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + inputs = ["flower","flow","flight"] |
| 79 | + output = "fl" |
| 80 | + res = longestCommonPrefix(strs=inputs) |
| 81 | + if res == output: |
| 82 | + print("It is ok!") |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
0 commit comments