Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
Find the most common substring in an array of strings with a given sequence length

I hope all is well on your end!

I encountered an interesting sequence problem and I've been struggling to solve it: "Find the most common sequence across the array of strings with a specific number of characters."

Input: (["abc", "usbc", "bcde"], 2) Output: "bc"

Input: (["terrific, specific"], 4) Output: "ific"

Here's what I have so far:

function commonSubstring(array, length) {
  let popular = '';
  let seq = []
  let map = {}

  for (let i = 0; i < array.length; i++) {
    for (let j = 0; j < array[i].length; j++) {
      const str = array[i].slice(j, j+length)
      if (str.length === length && array[i].includes(str)) {
        seq.push(str)
      }
      if (array[i].includes(seq[j]) && !map[seq[j]]) {
        map[seq[j]] = 1
        // console.log(seq[j])
      } else {
        map[seq[j]]++
        j++
      }
    }
  }
  console.log(map)
  return popular
}

What I was trying to do is go through each string element and find common sequences and I added a map to have a point system, then ultimately find the key that has the most points and return that key.

Honestly, I'm a bit lost. How do I efficiently solve this problem?

Thank you!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

here is a solution using a for loop and recursive call

const getSequence = (list, length, check) => {
  if(!list.length || (check && check.length < length)) {
   return false;
  }
  let result = check || list[0];  //setting first item as a result
  let found = true;

  let i;
  for(i = 1; i<list.length; i++) {
    if(list[i].indexOf(result.substring(0, length)) == -1) {
      found = false;
      break;
    }
  }
  if(!found) {
     let val = getSequence(list, length, result.substring(1, result.length)) //calling same function by removing first char 
     if(val) {
      return val;
     }
  }
  else {
    return result.substring(0, length);
  }
}


console.log(getSequence(["abc", "usbcj", "bcdeh"], 2))
console.log(getSequence(["terrific", "specific"], 4))
console.log(getSequence(["test", "text"], 2))

about 4 years ago · Juan Pablo Isaza Relatório

0

This is a modification of your code that implements the suggestions in my comment above:

  • Shorten the inner (j) loop to only loop through offsets that can hold the length in the current string,
  • Remove the length checks, as they are no longer needed,
  • Remove the checks for the substring in the array as they are unneeded.

I also added the max-search so that it would return the answer:

function commonSubstring(array, length) {
  let popular = '';
  let seq = []
  let map = {}

  for (let i = 0; i < array.length; i++) {
    for (let j = 0; j < array[i].length - length + 1; j++) {
      const str = array[i].slice(j, j+length)
      if (!map[str]) {
        map[str] = 1
      } else {
        map[str]++
      }
    }
  }
  // console.log(map)
  
  let maxv = 0;
  for(let prop in map) {
    if(map[prop] > maxv) {
      popular = prop;
      maxv = map[prop];
    }   
  }
  return popular
}

console.log(commonSubstring(["abc", "usbcj", "bcdeh"], 2))
console.log(commonSubstring(["terrific", "specific"], 4))
console.log(commonSubstring(["test", "text"], 2))

This runs in O(n*m) where n is the total number of input characters and m is the length parameter. As far as I know this is the best CPU complexity you can get for this problem. As for implementation efficiency, this should be pretty close to the best possible.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda