-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
29 lines (29 loc) · 842 Bytes
/
Solution.cs
File metadata and controls
29 lines (29 loc) · 842 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
namespace LeetCode.Problem387{
//87. First Unique Character in a String
//https://leetcode.com/problems/first-unique-character-in-a-string/
/*
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
*/
public class Solution {
public int FirstUniqChar(string s) {
Dictionary<char,int> dicS = new Dictionary<char,int>();
for (int i = 0; i < s.Length; i++)
{
var value = s[i];
if (dicS.ContainsKey(value))
{
dicS[value] = i;
continue;
}
else
{
dicS.Add(value,i);
var word = s.Substring(i+1);
if (!word.Contains(value))
return i;
}
}
return -1;
}
}
}