Created
October 20, 2020 01:49
-
-
Save TechWithTy/a15912409973d3d339ab82d6283eb425 to your computer and use it in GitHub Desktop.
Longest Common Prefix LeetCode
This file contains hidden or 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
/** 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