🎖️ Day 2: Common Prefix

easyarraysStringsprefixmatching

We are given an array of strings of size $$$N$$$. We need to report the length of the longest string which is present as a prefix in each of the strings of the array.

If no such common prefix exists, output -1.

A prefix of a string is any leading contiguous part of the string. For example, the string "abcde" has the following prefixes: "a", "ab", "abc", "abcd", "abcde".

Input Format:

The first line contains a single integer $$$N \ (1 \leq N \leq 10^5)$$$. This is followed by $$$N$$$ strings.

Let the length of each string be $$$M \ (1 \leq M \leq 30)$$$.

Output Format:

Output the length of the longest common prefix string among all the given strings. If there is no common prefix, output -1.

Examples:

Example 1:

Input:

4
AlgoUniversity
Algo
Algorithm
AlgoChamps

Output:

4

Example 2:

Input:

3
abc
def
ghi

Output:

-1

Note:

Prefix Algo is Present in each of the string. so, the Output is $$$4$$$.

Code

Loading Editor...