Longest Common Prefix

Longest common prefix was tough. It took a lot of Googling and Youtube to figure out exactly what I needed. For starters we need something to compare to. For this I set a variable of prefix to an empty string.

let prefix = ‘ ’

To take care of the event were we receive an empty string I set an if statement to return prefix if strs.length === 0

Next step is the for let loop to iterate through the strs variable.

Very standard loop, nothing crazy.

Once inside the loop we need to start iterating through each letter. This called for a placeholder and another loop.

Finally we just need to actually compare the letters like so.

If [j][i] did not match character we would return prefix if it did we add the letter to prefix by prefix + character.

--

--