#C88. Longest Common Word Prefix
Longest Common Word Prefix
Longest Common Word Prefix
Given a list of strings, each representing a sequence of words separated by spaces, your task is to find the longest common prefix that forms complete words from the beginning of each string.
For example, if the input strings are:
hello world programming
hello there
hello everyone
then the longest common word prefix is hello
.
If no non-empty common prefix exists that matches whole words, output an empty string.
Note: The common prefix must match entire words and the matching is performed from the beginning of each string.
You can assume that input strings do not have leading or trailing spaces.
inputFormat
The input is read from stdin
and has the following format:
- An integer n representing the number of strings.
- Followed by n lines, each line contains a string.
For example:
3 hello world hello there hello everyone
outputFormat
The output is written to stdout
and should be the longest common prefix (composed of complete words). If there is no common prefix, output an empty line.
3
hello world
hello there
hello everyone
hello