Created
January 3, 2021 11:12
-
-
Save darkcris1/a76589549a016970d912ad1ce56f4b8d to your computer and use it in GitHub Desktop.
match a longprefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function longPrefix(arr) { | |
let result = '' | |
if (arr.length === 0) return result | |
for (let i = 0; i < arr[0].length || 0; i++) { | |
for (let j = 1; j < arr.length; j++) { | |
if (arr[0][i] === arr[j][i]) { | |
if (j + 1 === arr.length) { | |
result += arr[0][i] | |
} | |
} else { | |
return result | |
} | |
} | |
} | |
return result | |
} | |
console.log(longPrefix(['outside', 'outdated'])) // out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment