-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest-common-prefix.js
More file actions
112 lines (89 loc) · 3.15 KB
/
longest-common-prefix.js
File metadata and controls
112 lines (89 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Problem: Longest Common Prefix
* Link: https://leetcode.com/problems/longest-common-prefix/
* Difficulty: Easy
*
* Find the longest common prefix string amongst an array of strings.
* If there is no common prefix, return an empty string "".
*
* Example:
* Input: strs = ["flower","flow","flight"]
* Output: "fl"
*
* Time Complexity: O(S) where S is the sum of all characters in all strings
* Space Complexity: O(1)
*/
// JavaScript Solution
function longestCommonPrefix(strs) {
if (!strs || strs.length === 0) return "";
// Start with the first string as the prefix
let prefix = strs[0];
// Compare with each string
for (let i = 1; i < strs.length; i++) {
// Reduce prefix length until it matches the beginning of strs[i]
while (strs[i].indexOf(prefix) !== 0) {
prefix = prefix.substring(0, prefix.length - 1);
if (prefix === "") return "";
}
}
return prefix;
}
// Alternative solution - vertical scanning
function longestCommonPrefixVertical(strs) {
if (!strs || strs.length === 0) return "";
// Check character by character
for (let i = 0; i < strs[0].length; i++) {
const char = strs[0][i];
// Check if this character exists at position i in all strings
for (let j = 1; j < strs.length; j++) {
if (i === strs[j].length || strs[j][i] !== char) {
return strs[0].substring(0, i);
}
}
}
return strs[0];
}
// Test cases
console.log(longestCommonPrefix(["flower","flow","flight"])); // "fl"
console.log(longestCommonPrefix(["dog","racecar","car"])); // ""
console.log(longestCommonPrefix(["interspecies","interstellar","interstate"])); // "inters"
module.exports = longestCommonPrefix;
/* Python Solution (commented):
def longest_common_prefix(strs: list[str]) -> str:
"""
Find the longest common prefix among an array of strings
Args:
strs: List of strings
Returns:
Longest common prefix string
"""
if not strs:
return ""
# Start with the first string as prefix
prefix = strs[0]
# Compare with each string
for i in range(1, len(strs)):
# Reduce prefix length until it matches the beginning of strs[i]
while not strs[i].startswith(prefix):
prefix = prefix[:-1]
if not prefix:
return ""
return prefix
# Alternative Python solution - vertical scanning
def longest_common_prefix_vertical(strs: list[str]) -> str:
"""Using vertical scanning approach"""
if not strs:
return ""
# Check character by character
for i in range(len(strs[0])):
char = strs[0][i]
# Check if this character exists at position i in all strings
for j in range(1, len(strs)):
if i == len(strs[j]) or strs[j][i] != char:
return strs[0][:i]
return strs[0]
# Test cases
print(longest_common_prefix(["flower","flow","flight"])) # "fl"
print(longest_common_prefix(["dog","racecar","car"])) # ""
print(longest_common_prefix(["interspecies","interstellar","interstate"])) # "inters"
*/