Write a function to find the longest common prefix string amomgst an array of strings.
If there is no common prefix, return an empty string "".
#region LeetCode 14 Longest Common Prefix
/*
* Write a function to find the longest common prefix string amomgst an array of strings.
* If there is no common prefix, return an empty string "".
*
* Input : strs = ["flower", "flow", "flight"]
* Output : "fl"
*
* Input : strs = ["dog", "racecar", "car"]
* Output : ""
*/
public string LeetCode_14(string[] strs)
{
string ans = "";
int c = 0;
bool Prefix = true;
if (strs.Length == 0)
return "";
if (strs.Length == 1)
return strs[0];
while (Prefix)
{
for (int i = 0; i < strs.Length-1; i++)
{
try
{
if(strs[i].Substring(c,1) != strs[i+1].Substring(c, 1))
{
Prefix = false;
break;
}
}
catch(Exception)
{
Prefix = false;
}
}
c++;
}
ans = strs[0].Substring(0, c - 1);
return ans;
}
#endregion