File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments