LeetCode problem#14
Longest Common Prefix
Solved in 1 languageC#
Solution
15 lines
1public class Solution {
2 public string LongestCommonPrefix(string[] strs) {
3 string prefix = strs[0];
4
5 for (int i = 1; i < strs.Length; i++)
6 {
7 while (!strs[i].StartsWith(prefix))
8 {
9 prefix = prefix.Substring(0, prefix.Length - 1);
10 }
11 }
12
13 return prefix;
14 }
15}Want a walkthrough of this C# solution?
Get a step-by-step explanation of the idea behind the code.
Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.