Longest Common Prefix

EasyTries (Prefix Trees)

Given an array of strings strs, return the longest common prefix: the longest string that every element of strs starts with.

If the strings have no common prefix (for example, their first characters differ, or one of them is empty), return the empty string "".

Constraints

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • Every string contains lowercase English letters only.

Examples

Example 1

Input: strs = ["interview","internet","interval","internal"]
Output: "inter"

All four start with "inter"; at the next position they split into v, n, v and n.

Example 2

Input: strs = ["tree","trie","trip"]
Output: "tr"

"tr" is shared; then "tree" has e where the others have i.

Example 3

Input: strs = ["apple","banana"]
Output: ""

The first letters already differ, so the common prefix is the empty string.

JavaScript you'll use: Strings and characters Map and Set

/**
 * @param {string[]} strs
 * @return {string}
 */
function longestCommonPrefix(strs) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.