Skip to the code
cf.
+10 XP

#367 Shared Start of Import Paths

easy

A refactoring tool wants to suggest a common folder for a group of import paths. As a first step, it needs the longest string that every path starts with.

Write sharedStart(paths) that returns the longest common prefix of all strings in paths (it may end in the middle of a folder name). If paths is empty, or the strings share no first character, return "".

Example 1

Input: paths = ["src/ui/button.js","src/ui/badge.js","src/utils.js"]
Output: "src/u"

All three start with src/u; the next characters are i, i, and t.

Example 2

Input: paths = ["lib/a.js","src/a.js"]
Output: ""

The first characters already differ.

Constraints

  • 0 <= paths.length <= 200
  • 0 <= paths[i].length <= 200
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).