Boundary handling · Easy
A file-grouping tool suggests the beginning shared by every supplied name. It must preserve original spelling and may return an empty prefix. A name ending early limits the prefix even if all other names continue with matching characters.
Return the longest case-sensitive prefix shared by every supplied name without changing or reordering the names.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ words: string[] }
Fields
- words is an array of names; empty names and duplicates are allowed.
- Strings contain printable ASCII characters only. Comparisons are case-sensitive; spaces count as characters.
Output
Return value
- Return a string prefix. An empty array returns an empty string; one name returns that entire name.
- If any name is empty or the first characters disagree, return an empty string.
Constraints
- 0 <= words.length <= 10000; the sum of all name lengths is at most 200000.
Examples
Example 1
{
"words": [
"service-api",
"service-web",
"service"
]
}Returns
"service"Example 2
{
"words": [
"App",
"apple"
]
}Returns
""Required complexity
O(total input characters + number of words) time and O(1) auxiliary space excluding the returned prefix