-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongestCommonPrefix.js
61 lines (50 loc) · 1.38 KB
/
longestCommonPrefix.js
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
// const longestCommonPrefix = strs => {
// let hash = {};
// for (let i = 0; i < strs.length; i++) {
// const word = strs[i];
// let curr = '';
// for (let j = 0; j < word.length; j++) {
// curr += word[j];
// if (hash[curr]) {
// hash[curr]++;
// } else {
// hash[curr] = 1;
// }
// }
// }
// let prefix = '';
// for (let word in hash) {
// if (hash[word] === strs.length && word.length > prefix.length) {
// prefix = word;
// }
// }
// return prefix;
// };
const longestCommonPrefix = strs => {
let prefix = '';
if (!strs.length) return prefix;
prefix = strs[0];
for(let i = 0; i < strs.length; i++) {
while (strs[i].indexOf(prefix) !== 0) {
prefix = prefix.substring(0, prefix.length - 1);
}
if (!prefix) break;
}
return prefix;
}
console.log(longestCommonPrefix(["flower","flow","flight"])); // 'fl'
console.log(longestCommonPrefix(["dog","racecar","car"])); // ''
// specification
// input: array of strings
// output: longest common prefix amongst strings
// constraints: if no common prefix, return an empty string
// edge cases:
// justification
// to find the longest common prefix in an array of strings
// explanation
// the array of strings will determine what the common prefix is, if any
// visualization
// approximation
// declare hash
// verification
// implementation