Skip to content

Instantly share code, notes, and snippets.

@TechWithTy
Created October 20, 2020 01:49
Show Gist options
  • Save TechWithTy/a15912409973d3d339ab82d6283eb425 to your computer and use it in GitHub Desktop.
Save TechWithTy/a15912409973d3d339ab82d6283eb425 to your computer and use it in GitHub Desktop.
Longest Common Prefix LeetCode
/** Initial Hypothesis
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
let splitWords = [];
let commonPrefix =[];
strs.forEach((word,i) =>{
splitWords[i] = word.split('');
})
if( splitWords !== undefined || splitWords.length > 0){
for(i=0; i < splitWords[0].length; i ++){
if(splitWords[0][i] == splitWords[1][i] && splitWords[0][i] == splitWords[2][i]){
commonPrefix.push(splitWords[0][i])
console.log(commonPrefix)
}else{
break;
}
}
}
return (commonPrefix === undefined || commonPrefix.length == 0 ? commonPrefix = "" : commonPrefix.join(''))
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment