Find the Shared Starting String
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
Overview: Find the longest starting string shared by every array element, including inputs with empty strings or no nonempty common prefix.
Constraints
- 1 <= words.length <= 200
- 0 <= words[i].length <= 200
- Every nonempty words[i] consists only of lowercase English letters a-z
Examples
Input: (['interact', 'internet', 'internal'],)
Expected Output: 'inter'
Input: (['sun', 'moon'],)
Expected Output: ''
Hints
- The common prefix can never be longer than any single word, so the first word bounds the answer.
- Scan character positions from left to right and stop at the first position where some word either ends or has a different character.
- An empty string anywhere in the array forces the answer to be empty.
Community answers
Answer by EZ