Sobes.tech
Intern — Senior

Determining the Longest Common Prefix of a String

livecode

Task condition

You are given an array of strings. Implement a method that returns the longest common prefix shared by all elements in the array. If there is no such prefix, the method should return an empty string.

Example 1: ["test", "testabc", "testf", "test1"] → "test"

Example 2: ["yu", "aero", "kekwao"] → ""

class Task {
    public String longestCommonPref(String[] words) {
        return "";
    }
}