-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
36 lines (26 loc) · 917 Bytes
/
Solution.cs
File metadata and controls
36 lines (26 loc) · 917 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
33
34
35
36
namespace LeetCode.Problem2351{
//First Letter to Appear Twice
//https://leetcode.com/problems/first-letter-to-appear-twice/
/*
Given a string s consisting of lowercase English letters, return the first letter to appear twice.
*/
public class Solution {
public char RepeatedCharacter(string s) {
Dictionary<char,List<int>> word = new Dictionary<char, List<int>>();
for (int i = 0;i < s.Length; i++){
if (word.Where(p => p.Key == s[i]).Any()){
return s[i];
}
else {
word.Add(s[i],new List<int>() {i});
}
}
var index = word
.Where(p => p.Value.Count > 1 )
.Select(p => p.Value[1])
.OrderBy(p => p)
.FirstOrDefault();
return s[index];
}
}
}