Sobes.tech
Junior — Senior

Finding the longest common prefix in an array of strings

livecode

Task condition

The method takes an array of strings and returns the longest prefix that appears at the beginning of each string in the array. If there is no common prefix, an empty string is returned.

Examples: ["ab", "abc", "abcde", "abf"] → "ab" ["cat", "dog", "bird"] → ""

class Test {
    public String longestPrefix(String[] arr) {
        // TODO
    }
}