Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Haleshot committed Aug 24, 2024
1 parent 25310ba commit 1c916c0
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions Leetcode/Longest_Common_Prefix/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
# 14. Longest Common Prefix (Easy)
# Longest Common Prefix (Easy)

## Table of Contents

- [Problem Statement](#problem-statement)
- [Examples](#examples)
- [Constraints](#constraints)
- [Approach](#approach)
- [Solutions](#solutions)
- [Solution 1: Sorting and Comparing](#solution-1-sorting-and-comparing)
- [Solution 1: Sorting Approach](#solution-1-sorting-approach)
- [Solution 2: Character-by-Character Comparison](#solution-2-character-by-character-comparison)
- [Complexity Analysis](#complexity-analysis)
- [Code Explanation](#code-explanation)
- [Related Resources](#related-resources)
- [Additional Resources](#additional-resources)

## Problem Statement

[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/description/)
[Longest Common Prefix - LeetCode](https://leetcode.com/problems/longest-common-prefix/description/)

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 `""`.
If there is no common prefix, return an empty string "".

## Examples

Expand All @@ -40,24 +39,21 @@ Explanation: There is no common prefix among the input strings.

## Constraints

- $1 \leq strs.length \leq 200$
- $0 \leq strs[i].length \leq 200$
- `strs[i]` consists of only lowercase English letters.

## Approach

We'll explore two different approaches to solve this problem:

1. Sorting and comparing the first and last strings.
2. Character-by-character comparison of all strings.
- $$1 \leq \text{strs.length} \leq 200$$
- $$0 \leq \text{strs[i].length} \leq 200$$
- $$\text{strs[i]}$$ consists of only lowercase English letters.

## Solutions

### Solution 1: Sorting and Comparing
### Solution 1: Sorting Approach

```python
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
ans = ""
strs = sorted(strs)
min_len, max_len = strs[0], strs[-1]
Expand All @@ -73,7 +69,12 @@ class Solution(object):
```python
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
min_length = float('inf')

for s in strs:
if len(s) < min_length:
min_length = len(s)
Expand All @@ -90,33 +91,36 @@ class Solution(object):

## Complexity Analysis

### Solution 1:
- Time Complexity: $O(N \log N + M)$, where N is the number of strings and M is the length of the shortest string.
- Space Complexity: $O(1)$ (excluding the space used for sorting)
### Solution 1: Sorting Approach
- Time Complexity: $$O(N \log N + M)$$, where $$N$$ is the number of strings and $$M$$ is the length of the longest string.
- Space Complexity: $$O(1)$$ (ignoring the space used for sorting)

### Solution 2:
- Time Complexity: $O(S)$, where S is the sum of all characters in all strings.
- Space Complexity: $O(1)$
### Solution 2: Character-by-Character Comparison
- Time Complexity: $$O(S)$$, where $$S$$ is the sum of all characters in all strings.
- Space Complexity: $$O(1)$$

## Code Explanation

### Solution 1: Sorting and Comparing
1. Sort the input array of strings lexicographically.
2. Compare the first (lexicographically smallest) and last (lexicographically largest) strings.
3. Build the common prefix until a mismatch is found or we reach the end of either string.
### Solution 1: Sorting Approach

1. Sort the array of strings lexicographically.
2. Compare the first and last strings in the sorted array.
3. The longest common prefix will be the common prefix of these two strings.

### Solution 2: Character-by-Character Comparison

1. Find the length of the shortest string in the array.
2. Compare characters at each position for all strings.
3. If a mismatch is found, return the common prefix up to that point.
4. If no mismatch is found, return the entire shortest string.
2. Iterate through the characters of the first string up to the length of the shortest string.
3. For each character, compare it with the corresponding character in all other strings.
4. If a mismatch is found, return the prefix up to that point.
5. If no mismatch is found, return the entire shortest string as the longest common prefix.

## Related Resources
## Additional Resources

- [YouTube Explanation](https://www.youtube.com/watch?v=8C6F8_nM0qs)
- [GitHub Solution](https://github.com/gahogg/Leetcode-Solutions/blob/main/Longest%20Common%20Prefix%20-%20Leetcode%2014/Longest%20Common%20Prefix%20-%20Leetcode%2014.py)
- [LeetCode Solution](https://leetcode.com/problems/longest-common-prefix/solutions/5684830/solution/)
- [Personal Submission](https://leetcode.com/submissions/detail/1366878699/)
- [YouTube Explanation - Algomap](https://www.youtube.com/watch?v=8C6F8_nM0qs)
- [GitHub - Leetcode Solutions](https://github.com/gahogg/Leetcode-Solutions/blob/main/Longest%20Common%20Prefix%20-%20Leetcode%2014/Longest%20Common%20Prefix%20-%20Leetcode%2014.py)
- [LeetCode Solution Explanation](https://leetcode.com/problems/longest-common-prefix/solutions/5684830/solution/)
- [Personal Submission Details](https://leetcode.com/submissions/detail/1366878699/)

> [!NOTE]
> This problem is part of a larger collection following the roadmap on [algomap.io](https://algomap.io/). For more details and related problems, please refer to the AlgoMap website.

0 comments on commit 1c916c0

Please sign in to comment.