C# Solution


  • 0
            public string LongestCommonPrefix(string[] strs)
            {
                if (strs == null || strs.Length == 0)
                    return string.Empty;
                else if (strs.Length == 1)
                    return strs[0];
    
                bool loopContinue = true;
                int charIndex = 0;
                char currentChar;
                StringBuilder prefix = new StringBuilder();
    
                while (true)
                {
                    if (charIndex <= strs[0].Length - 1)
                        currentChar = strs[0][charIndex];
                    else
                        break;
    
                    for (int stringIndex = 1; stringIndex <= strs.Length - 1; stringIndex++)
                    {
                        if (charIndex > strs[stringIndex].Length - 1 || strs[stringIndex][charIndex] != currentChar)
                        {
                            loopContinue = false;
                            break;
                        }
                    }
    
                    if (!loopContinue)
                        break;
    
                    prefix.Append(currentChar);
                    charIndex++;
                }
    
                return prefix.ToString();
            }

Log in to reply
 

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