Comments

Roi Horowitz 5 days ago

``const solve = (strArray) => {

const len = strArray.length;

if (len === 0) throw new Error("Empty Array");

if (len === 1) return strArray[0];

const minLength = Math.min(strArray[0].length, strArray[len-1].length);

let i = 0; while (i < minLength && strArray[0][i] === strArray[len-1][i]) { i++; }

let longestPrefix = strArray[0].substring(0, i); return longestPrefix; };

Roi Horowitz 5 days ago

`const solve = (strArray) => {

const len = strArray.length;

if (len === 0) throw new Error("Empty Array");

if (len === 1) return strArray[0];

const minLength = Math.min(strArray[0].length, strArray[len-1].length);

let i = 0; while (i < minLength && strArray[0][i] === strArray[len-1][i]) { i++; }

let longestPrefix = strArray[0].substring(0, i); return longestPrefix; };`

Roi Horowitz 5 days ago

const solve = (strArray) => {

const len = strArray.length;

if (len === 0) throw new Error("Empty Array");

if (len === 1) return strArray[0];

const minLength = Math.min(strArray[0].length, strArray[len-1].length);

let i = 0; while (i < minLength && strArray[0][i] === strArray[len-1][i]) { i++; }

let longestPrefix = strArray[0].substring(0, i); return longestPrefix; };

Danylo Vershkov14 days ago

const solve = (strArray) => { let currentChar = ''; let pref = ''; for(let i = 0; i<strArray[0].length; i++) { currentChar=strArray[0][i]; for(let j = 1; j<strArray.length; j++) { if(currentChar !== strArray[j][i]) { return pref; } } pref+=currentChar; } return pref; };

ROI overriding Regional18 days ago

i think compiler has a problem with this, because the solution works fine

const solve = (strArray) => { let res = ""; for (let i = 0; i < strArray[0].length; i++) { if (strArray[0][i] === strArray[1][i]) res += strArray[0][i]; else break; } return res; };

BoilingSoup22 days ago

const solve = (strArray) => {
  let longest = "";
  for(str of strArray){
    if(str.length > longest.length){
      longest = str;
    }
  }
  
  let j = 0;
  
  for(let i = 0; i < longest.length; i++){
    if(strArray.every(el => el[i] === longest[i])){
      j++;
      continue;
    } else {
      break;
    }
  }
  
  return longest.slice(0, j);
};

Karan Kumar 4 months ago

const solve = (strArray) => {
  let map = {}
  let flag = true;
  for (let i=0; i<strArray[0].length; i++) {
     for (let j=0; j<strArray.length; j++) {
       let element = strArray[j];
       map = {...map, [j]: element[i]}
     }
	  let v = Object.values(map)[0]
	  Object.values(map).map(e => {
		  if (e !== v) v = false;
	  })
	  if (!v) {
		  console.log(strArray[0].slice(0,i))
		  break;
	  }
  }
};

calculating feed4 months ago

const solve = (strArray) => { let prefix = ''; const maxLength = Math.min(...strArray.map(str => str.length)); for(let i=0;i<maxLength;i++){ prefix = strArray[0].substring(0,i+1); for(let j=0; j< strArray.length; j++){ const pre = strArray[j].substring(0,i+1); if(prefix !== pre){ return prefix.substring(0,i); } } } return prefix };

Tuong Vi Pham5 months ago

const solve = (strArray) => {
  return strArray.reduce((acc, cur) => {
    acc = acc
      ? acc
          .split('')
          .reduce((innerAcc, innerCur, index) => {
            if (innerCur === cur[index]) innerAcc.push(innerCur);
            return innerAcc;
          }, [])
          .join('')
      : cur;
    return acc;
  }, null);
};

Panji Ahmad Nurhusni5 months ago

A Python solution.

class Solution:
  def solve(self, str_array):
    compared_word = ""
    
    for i in range(1, len(str_array)):
      prefix = ""
      if compared_word == "":
        compared_word = str_array[0]
      for j in range(len(compared_word)):
        if compared_word[j] == str_array[i][j]:
          prefix += str_array[0][j]
        else:
          break
      compared_word = prefix
          
    return compared_word

Mobility Grenada6 months ago

const solve = (strArray) => { let word = strArray[0];

for(let i = 0; i < strArray.length; i++) { if (!strArray[i].startsWith(word)) { word = word.slice(0, -1); i = 0; } else { if (i === strArray.length - 1 || word === '') { break } } }
return word; };

Sarthak Joshi7 months ago

const solve = (strArray) => { let str = '' for (let i = 0; i < strArray.length - 1; i++) { str = '' for (let j = 0; j < strArray[i].length; j++) { if (strArray[i][j] === strArray[i + 1][j]) { str += strArray[i][j] } } } return str };

njboot10 months ago

const solve = strArray => {
  // find shortest word, that's the maximum amount of letters we need to compare against
  const shortest = strArray
    .reduce(
      (shortest, str) =>
        str.length < shortest.length ? (shortest = str) : shortest,
      new Array(100000)
    )
    .split("");
  // common arr to later return w .join("")
  let common = [];
  // columns (equal to shortest length word)
  for (let col = 0; col < shortest.length; col++) {
    // set flags and letter variable
    let same = true;
    let letter = "";
    // rows (equal to number of words). We loop through each column, incrementing one row at a time
    for (let row = 0; row < strArray.length; row++) {
      // set letter equal to strArray[row][col]
      letter = strArray[row][col];
      // check if all letters in col are equal
      if (letter !== strArray[0][col]) {
        same = false;
      }
    }
    // if all letters in col equal, push that letter to common []
    if (same) {
      common.push(letter);
    }
  }
  return common.join("");
};

Diogo Magrini Furmann10 months ago

const solve = (strArray) => {
  // if second string is undefinned then return first string (stop recursion)
  if (!strArray[1]) return strArray[0];

    [str1, str2, ...rest] = strArray;
    let res = '';

    // loop that increments res untils the longest common prefix
    for (let i = 0; i < str1.length; i++) {
        if (str1[i] !== str2[i]) break;
        res += str1[i];
    }

    // recursive call having as param the current answer plus the rest of the array
    return solve([res, ...rest]);
};

Forgesa year ago

const solve = strArray => {
  const word = strArray.sort((a, b) => a.length - b.length)[0];
  let result = [];

  strArray.forEach((item, index) => {
    for (let i = 0; i < word.length; i++) {
      if (strArray[index].startsWith(word.slice(0, i + 1))) {
        result[index] = word.slice(0, i + 1);
      }
    }
  });

  return result.sort((a, b) => a.length - b.length)[0];
};

Brunoa year ago

const solve = (strArray) => { const dict = strArray.reduce((prevDict, str) => { for (let i=1; i <= str.length; i++) { prevDict[str.substr(0,i)] = (prevDict[str.substr(0,i)] || 0) + 1; } return prevDict; }, {}); const str = Object.keys(dict).reduce((prevKey, key) => { return (dict[prevKey] > dict[key]) ? prevKey : key; }, Object.keys(dict)[0]); return str; };

Mauritius Rupee Oklahoma2 years ago

const solve = (strArray) => {
  let prefix;
  let counter = 1;
  const minStrLength = Math.min(...strArray.map(str => str.length));

  for (let i = 0; i < minStrLength; i++) {
    const allMatch = strArray.every((str) => {
      return str.slice(0, counter) === strArray[0].slice(0, counter);
    });
    if (allMatch) {
      prefix = strArray[0].slice(0, counter);
      counter++;
    }
  }

  return prefix;
};

Abdelhamid Ismail2 years ago

const solve = (strArray) => {
  let prefix = "";
  for (let idx = 0; idx < strArray[0].length; idx++) {
    let add = true;
    for (let a = 0; a < strArray.length; a++) {
      if (strArray[a][idx] !== strArray[0][idx]) {
        add = false;
      }
    }
    add ? (prefix = prefix + strArray[0][idx]) : null;
  }
  return prefix;
};

Abdelhamid Ismail2 years ago

const solve = (strArray) => {
  let prefix = "";
  for (let idx = 0; idx < strArray[0].length; idx++) {
    let add = true;
    for (let a = 0; a < strArray.length; a++) {
      if (strArray[a][idx] !== strArray[0][idx]) {
        add = false;
        break
      }
    }
    if (add) {
      prefix = prefix + strArray[0][idx]
    } else {
      break
    }
  }
  return prefix;
};