Intermediate Level
Implement the method longestCommonPrefix that returns the shared starting text of all strings.
Return the longest prefix that appears at the beginning of every string in the input list.
The task is designed to test careful handling of edge cases, not only the most common input.
- Use only the first
sizestrings. - The input contains at least one string.
- Comparison is case-sensitive.
Example 1
Input
words (string[]) =
["flower","flow","flight"]size (int) =
3Returns (string)
flExample 2
Input
words (string[]) =
["interview","internet","internal"]size (int) =
3Returns (string)
inteExample 3
Input
words (string[]) =
["same","same"]size (int) =
2Returns (string)
sameStart with the first string as the prefix and shorten it until every string starts with it.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run