-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestCommonPrefix.cs
52 lines (41 loc) · 1.52 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
using System.Text;
namespace LeetCode;
internal sealed class LongestCommonPrefix {
static Solution solve = new();
class Solution {
public string LongestCommonPrefix(string[] strs) {
StringBuilder result = new();
var skip = new HashSet<int>();
int longest = strs.OrderBy(x => x.Length).First().Length;
int hits = -1;
Dictionary<char, List<int>> charHits;
for(int c = 0; c < longest || hits == 0; c++) {
hits = 0;
charHits = new Dictionary<char, List<int>>();
for(int i = 0; i < strs.Length; i++) {
if(skip.Contains(i)) { continue; }
if(i >= strs[i].Length && !skip.Contains(i)) {
skip.Add(i);
continue;
}
if(charHits.ContainsKey(strs[i][c])) {
charHits[strs[i][c]].Add(i);
} else {
charHits.Add(strs[i][c], new List<int>() { i });
}
}
var matchLines = charHits
.Where(x => x.Value.Count == 1)
.Select(x => x.Value)
.SelectMany(x => x);
hits = matchLines.Count();
if(hits < 2) {
}
foreach(int line in matchLines) {
skip.Add(line);
}
}
return result.ToString();
}
}
}