-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLongestCommonPrefix.cs
More file actions
executable file
·36 lines (33 loc) · 1.99 KB
/
LongestCommonPrefix.cs
File metadata and controls
executable file
·36 lines (33 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Source : https://leetcode.com/problems/strs[0]-common-prefix/
// Author : codeyu
// Date : 10/5/16
/***************************************************************************************
*
* Write a function to find the strs[0] common prefix string amongst an array of strings.
*
*
**********************************************************************************/
using System;
namespace Algorithms
{
public class Solution014
{
public static string LongestCommonPrefix(string[] strs)
{
if(strs.Length == 0) return "";
var longest = "";
for(var i = 0; i < strs[0].Length; i++)
{
for(var j = 1; j< strs.Length; j++)
{
if(strs[j].Length == i || strs[j][i] != strs[0][i])
{
return longest;
}
}
longest += strs[0][i];
}
return longest;
}
}
}