Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions logic-exercises/exercise-day-4/longestCommon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function longestCommon(strs) {
if (strs.length === 0)
return "";
var commonPrefix = "";
for (var i = 0; i < strs[0].length; i++) {
var currentChar = strs[0][i];
var areAllCharsSame = true;
for (var j = 0; j < strs.length; j++) {
if (strs[j][i] !== currentChar) {
return commonPrefix;
}
}
if (currentChar) {
commonPrefix += currentChar;
}
}
return commonPrefix;
}
console.log(longestCommon(["Palmeiras", "Palermo"]));
console.log(longestCommon(["porco", "gamba"]));
Empty file.
24 changes: 24 additions & 0 deletions logic-exercises/exercise-day-4/longestCommon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function longestCommon(strs) {

if (strs.length === 0) return ""

let commonPrefix = ""
for (let i = 0; i < strs[0].length; i++) {
let currentChar = strs[0][i]
let areAllCharsSame = true

for (let j = 0; j < strs.length; j++) {
if(strs[j][i] !== currentChar) {
return commonPrefix
}
}

if(currentChar) {
commonPrefix += currentChar
}
}
return commonPrefix
}

console.log(longestCommon(["Palmeiras", "Palermo"]))
console.log(longestCommon(["porco", "gamba"]))
13 changes: 13 additions & 0 deletions logic-exercises/exercise-day-4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "exercise-day-4",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start":"clear && tsc --outFile longestCommon.js longestCommon.ts && node longestCommon.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}