forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
32 lines (30 loc) · 982 Bytes
/
Solution.cs
File metadata and controls
32 lines (30 loc) · 982 Bytes
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
namespace LeetCodeNet.G0001_0100.S0014_longest_common_prefix {
// #Easy #Top_Interview_Questions #String #Level_2_Day_2_String #Udemy_Strings
// #Top_Interview_150_Array/String #Big_O_Time_O(n*m)_Space_O(m)
// #2025_06_20_Time_0_ms_(100.00%)_Space_42.70_MB_(97.50%)
public class Solution {
public string LongestCommonPrefix(string[] strs) {
if (strs.Length < 1) {
return "";
}
if (strs.Length == 1) {
return strs[0];
}
string temp = strs[0];
int i = 1;
string cur;
while (!string.IsNullOrEmpty(temp) && i < strs.Length) {
if (temp.Length > strs[i].Length) {
temp = temp.Substring(0, strs[i].Length);
}
cur = strs[i].Substring(0, temp.Length);
if (!cur.Equals(temp)) {
temp = temp.Substring(0, temp.Length - 1);
} else {
i++;
}
}
return temp;
}
}
}