1186. Words in Every Line
A teacher collects short poems and wants the vocabulary shared by every one of them. Each element of lines is one poem line made of lowercase words separated by single spaces, with no leading or trailing space.
Return the distinct words that appear in every line, sorted in ascending lexicographic order. A word counts once per line however many times it repeats inside it, and the result contains each qualifying word once. If no word is shared by all lines, return an empty array.
Constraints: 1 <= lines.length <= 100 and 1 <= lines[i].length <= 1000. Build a set of words per line and intersect them as you go; the cost is linear in the total text length plus the sorting of the result.
Example 1
- Input:
- lines = ["red tea moon","moon red fox","red moon sea"]
- Output:
- ["moon","red"]
- Explanation:
Only red and moon appear in all three lines, and they sort as moon, red.
Example 2
- Input:
- lines = ["solo word"]
- Output:
- ["solo","word"]
- Explanation:
With a single line every distinct word of that line qualifies: solo and word.
Example 3
- Input:
- lines = ["a b","c d"]
- Output:
- []
- Explanation:
The two lines share no word, so the answer is empty.
Constraints
1 ≤ lines.length ≤ 100
1 ≤ lines[i].length ≤ 1000
lowercase words separated by single spaces
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(total length + k log k)
- Space
- O(total length)