Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 497d30c

Browse files
2 parents 2083ec5 + 9da65b7 commit 497d30c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcode387_FirstUniqueCharacterInAString
8+
{
9+
/// <summary>
10+
/// Leetcode 387 - First unique character in a string
11+
/// https://leetcode.com/problems/first-unique-character-in-a-string/description/
12+
/// </summary>
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
}
18+
19+
public int FirstUniqChar(string s)
20+
{
21+
if (s == null || s.Length == 0)
22+
return -1;
23+
24+
int SIZE = 26;
25+
var count = new int[SIZE];
26+
var length = s.Length;
27+
for (int i = 0; i < length; i++)
28+
{
29+
count[s[i] - 'a']++;
30+
}
31+
32+
// find first one with count = 1
33+
var hashset = new HashSet<char>(s.ToCharArray());
34+
for (int i = 0; i < length; i++)
35+
{
36+
var current = s[i];
37+
if (hashset.Contains(current))
38+
{
39+
if (count[current - 'a'] == 1)
40+
return i;
41+
42+
hashset.Remove(current);
43+
}
44+
}
45+
46+
return -1;
47+
}
48+
}
49+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /