-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path014-LongestCommonPrefix.cs
40 lines (36 loc) · 1.16 KB
/
014-LongestCommonPrefix.cs
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
36
37
38
39
40
//-----------------------------------------------------------------------------
// Runtime: 96ms
// Memory Usage: 24.6 MB
// Link: https://leetcode.com/submissions/detail/358762489/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _014_LongestCommonPrefix
{
public string LongestCommonPrefix(string[] strs)
{
if (strs.Length == 0) { return string.Empty; }
if (strs.Length == 1) { return strs[0]; }
var index = 0;
bool isSame = true;
var firstString = strs[0];
for (index = 0; index < firstString.Length; index++)
{
for (int i = 1; i < strs.Length; i++)
{
if (strs[i].Length <= index ||
strs[i][index] != firstString[index])
{
isSame = false;
break;
}
}
if (!isSame)
{
break;
}
}
return firstString.Substring(0, index);
}
}
}