My solution in C#


  • 0
    E
    public string LongestCommonPrefix(string[] strs) {
            if (strs == null || strs.Count() == 0)
                return "";
            
            int j = 0;
            for (; j < strs[0].Length; j++)
            {
                for (int i = 1; i < strs.Length; i++)
                {
                    if (strs[i].Length < j + 1 || strs[0][j] != strs[i][j])
                        return strs[0].Substring(0, j);
                }
            }
            
            return strs[0].Substring(0, j);
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.