14. Longest Common Prefix

This commit is contained in:
Пытков Роман
2024-01-30 23:47:49 +03:00
parent 66ea6b80f4
commit 1e43adf29e
5 changed files with 125 additions and 19 deletions

View File

@@ -0,0 +1,32 @@
using System.Text;
namespace _14._Longest_Common_Prefix;
public class Solution
{
public string LongestCommonPrefix(string[] strs)
{
if (strs.Length == 1)
return strs[0];
StringBuilder sb = new StringBuilder(256);
int maxLen = strs.Select(s => s.Length).Min();
for (int i = 0; i < maxLen; i++)
{
char letter;
if ((letter = strs[0][i]) != strs[1][i])
break;
for (int j = 2; j < strs.Length; j++)
{
if (letter != strs[j][i])
{
maxLen = -1;
break;
}
}
if(maxLen != -1)
sb.Append(letter);
}
return sb.ToString();
}
}