In the Leetcode Longest Common Prefix problem solution in C# programming Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string “”.
Leetcode Longest Common Prefix problem solution in C# programming
public class Solution {
public string LongestCommonPrefix(string[] strs) {
var prefix = "";
var firstWord = strs[0];
var curPrefix = "";
for(int i = 0; i < firstWord.Length; i++)
{
curPrefix += firstWord[i];
if(strs.All(s => s.StartsWith(curPrefix)))
prefix = curPrefix;
else
break;
}
return prefix;
}
}
Also read,
- Leetcode Longest Common Prefix problem solution in C++
- Leetcode Longest Common Prefix problem solution in Java
- Leetcode Longest Common Prefix problem solution in Python
- Leetcode Longest Common Prefix problem solution in C