From 662ade44fe4d04e67f35f63083487a41b6b747d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Marshall?= Date: Fri, 4 Feb 2022 11:14:17 -0300 Subject: [PATCH] Semana 22 - Maior Prefixo Comum --- semana22/maiorPrefixoComum/index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 semana22/maiorPrefixoComum/index.js 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