📅  最后修改于: 2023-12-03 15:29:45.850000             🧑  作者: Mango
C# LCP is a method used to find the longest common prefix between two or more strings.
The C# LCP method works by comparing the characters in each string starting from the beginning until two characters do not match. The length of the common prefix is returned.
string[] strings = {"flower", "flow", "flight"};
string prefix = LCP(strings);
Console.WriteLine(prefix); // Output: "fl"
public static string LCP(string[] strs)
{
if(strs == null || strs.Length == 0) return "";
for(int i=0; i<strs[0].Length; i++)
{
char c = strs[0][i];
for(int j=1; j<strs.Length; j++)
{
if(i == strs[j].Length || strs[j][i] != c)
{
return strs[0].Substring(0, i);
}
}
}
return strs[0];
}
The C# LCP method is a simple yet effective method for finding the longest common prefix between two or more strings. It has a linear time complexity and is a useful tool for many programming applications.