All files / leetCode 0014.ts

100% Statements 9/9
100% Branches 4/4
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  4x   4x 8x 21x   21x 3x       1x    
export default function longestCommonPrefix(stringArray: string[]): string {
  const firstString = stringArray[0]
 
  for (let len = 0; stringArray.length > 0; len++) {
    for (let i = 0; i < stringArray.length; i++) {
      const str = stringArray[i]
 
      if (str.length <= len || firstString[len] !== str[len])
        return str.slice(0, len)
    }
  }
 
  return ''
}