C# - no StringBuilder, just length.


  • 0
    public string LongestCommonPrefix(string[] strs) {
        if (strs == null || strs.Length == 0) return "";
        int length = 0;
        
        while (true)
        {
            if (length < strs[0].Length)
            {
                bool done = false;
                for (int i = 1; i < strs.Length; i++)
                {
                    if (strs[i].Length <= length || strs[i][length] != strs[0][length])
                    {
                        done = true;
                    }
                }
                if (done) break;
            }
            else
            {
                break;
            }
            
            length++;
        }
        
        return strs[0].Substring(0, length);
    }

Log in to reply
 

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