diff --git a/semana22/maiorPrefixoComum/index.js b/semana22/maiorPrefixoComum/index.js new file mode 100644 index 0000000..b5cc4b9 --- /dev/null +++ b/semana22/maiorPrefixoComum/index.js @@ -0,0 +1,20 @@ +function longestCommon(strs) { + if(strs.length === 0) return "" + + let commonPrefix = "" + for(let i = 0; i < strs[0].length; i++) { + let currentChar = strs[0][i] + + for(let j = 0; j < strs.length; j++) { + if(strs[j][i] !== currentChar) { + return commonPrefix + } + } + + if(currentChar) { + commonPrefix += currentChar + } + } + + return commonPrefix +}; \ No newline at end of file