#C159. Longest Common Prefix of File Paths

    ID: 44811 Type: Default 1000ms 256MiB

Longest Common Prefix of File Paths

Longest Common Prefix of File Paths

You are given a list of file paths represented as strings. Your task is to determine the longest common prefix among these paths.

The longest common prefix is defined as the longest string \(S\) such that every file path in the input starts with \(S\). In case there is no common prefix, output an empty string. Note that if the list of paths is empty, the answer should be an empty string.

This problem requires a careful iteration over the characters of the file paths, comparing them one by one until a mismatch is found across any two paths. Edge cases to consider include situations where:

  • The input list is empty.
  • There is only one file path.
  • The common prefix is very short (e.g., only a single character).

inputFormat

The input is read from standard input (stdin) and has the following format:

  1. The first line contains a single integer \(N\) (where \(0 \leq N \leq 100\)), representing the number of file paths.
  2. The next \(N\) lines each contain a file path as a non-empty string (with length at most 1000).

outputFormat

Output the longest common prefix among the given file paths using standard output (stdout). If there is no common prefix, print an empty string.

## sample
3
/home/user/docs
/home/user/photos
/home/user/music
/home/user/