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 2083ec5

Browse files
check in source code
1 parent fd3220c commit 2083ec5

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcdoe387_FindFirstUniqueCharactersInAString
8+
{
9+
/// <summary>
10+
/// Leetcode 387 - find first unique characters 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+
var result = FirstUniqChar("leetcode");
18+
}
19+
20+
/// <summary>
21+
/// time complexity: O(N)
22+
/// </summary>
23+
/// <param name="s"></param>
24+
/// <returns></returns>
25+
public static int FirstUniqChar(string s)
26+
{
27+
if (s == null || s.Length == 0)
28+
return -1;
29+
30+
int SIZE = 26;
31+
var count = new int[SIZE];
32+
var length = s.Length;
33+
for (int i = 0; i < length; i++)
34+
{
35+
count[s[i] - 'a']++;
36+
}
37+
38+
// find first one with count = 1
39+
var hashset = new HashSet<char>(s.ToCharArray());
40+
for (int i = 0; i < length; i++)
41+
{
42+
var current = s[i];
43+
if (hashset.Contains(current))
44+
{
45+
if (count[current - 'a'] == 1)
46+
return i;
47+
48+
hashset.Remove(current);
49+
}
50+
}
51+
52+
return -1;
53+
}
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcdoe387_FindFirstUniqueCharactersInAString
8+
{
9+
/// <summary>
10+
/// Leetcode 387 - find first unique characters 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+
var result = FirstUniqChar("leetcode");
18+
}
19+
20+
/// <summary>
21+
/// time complexity: O(N)
22+
/// </summary>
23+
/// <param name="s"></param>
24+
/// <returns></returns>
25+
public static int FirstUniqChar(string s)
26+
{
27+
if (s == null || s.Length == 0)
28+
return -1;
29+
30+
int SIZE = 26;
31+
var count = new int[SIZE];
32+
var length = s.Length;
33+
for (int i = 0; i < length; i++)
34+
{
35+
count[s[i] - 'a']++;
36+
}
37+
38+
// find first one with count = 1
39+
var hashset = new HashSet<char>(s.ToCharArray());
40+
for (int i = 0; i < length; i++)
41+
{
42+
var current = s[i];
43+
if (hashset.Contains(current))
44+
{
45+
if (count[current - 'a'] == 1)
46+
return i;
47+
48+
hashset.Remove(current);
49+
}
50+
}
51+
52+
return -1;
53+
}
54+
}
55+
}

0 commit comments

Comments
(0)

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