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

133
Visualizações
String trimming: Performance of while loop vs Binary Search?

The standard way of trimming some characters of a string, would be to loop through each character from left to right (or right to left depending on l or r trim) and then slice it. This should have worst-case complexity O(n), if n is the length of the string, but what really matters here is how many to-be-trimmed characters I have. If I have x, it'll be O(x).

I was thinking: If I wanted to trim A TON of strings, do you think it would be possible to improve on that performance by doing a binary search? It would definitely improve worst-case runtime to O(logn), but what I'm not so sure about is whether it would improve average case runtime. How can I analyze my runtime with respect to x instead of n here?

For the implementation, I thought of simply having (For left trim):

leftTrim(str, start, end, i):
    if end-start == 2:
        return str[i] is trimChar ? i : -1
    if str[i] is trimChar:
        leftTrim(str, i, end, i + (end-start)/2)
    else:
        leftTrim(str, start, i, i/2)
leftTrim(str, 0, str.length, str.length/2)

Although it looks a little bit overcomplicated to me, it should work, right?

about 4 years ago · Juan Pablo Isaza
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