File tree 1 file changed +19
-28
lines changed
src/main/java/com/fishercoder/solutions 1 file changed +19
-28
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
3
/**
4
- * 387. First Unique Character in a String
5
- * Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
6
-
7
- Examples:
8
-
9
- s = "leetcode"
10
- return 0.
11
-
12
- s = "loveleetcode",
13
- return 2.
14
- Note: You may assume the string contain only lowercase letters.
15
-
4
+ * 387. First Unique Character in a String Given a string, find the first non-repeating character in
5
+ * it and return it's index. If it doesn't exist, return -1.
6
+ *
7
+ * Examples:
8
+ *
9
+ * s = "leetcode" return 0.
10
+ *
11
+ * s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase letters.
16
12
*/
17
13
18
14
public class _387 {
19
-
15
+ public static class Solution1 {
20
16
public static int firstUniqChar (String s ) {
21
- int [] freq = new int [26 ];
22
- for (int i = 0 ; i < s .length (); i ++) {
23
- freq [s .charAt (i ) - 'a' ]++;
17
+ int [] freq = new int [26 ];
18
+ for (int i = 0 ; i < s .length (); i ++) {
19
+ freq [s .charAt (i ) - 'a' ]++;
20
+ }
21
+ for (int i = 0 ; i < s .length (); i ++) {
22
+ if (freq [s .charAt (i ) - 'a' ] == 1 ) {
23
+ return i ;
24
24
}
25
- for (int i = 0 ; i < s .length (); i ++) {
26
- if (freq [s .charAt (i ) - 'a' ] == 1 ) {
27
- return i ;
28
- }
29
- }
30
- return -1 ;
31
- }
32
-
33
- public static void main (String ... strings ) {
34
- String s = "leetcode" ;
35
- System .out .println (firstUniqChar (s ));
25
+ }
26
+ return -1 ;
36
27
}
37
-
28
+ }
38
29
}
You can’t perform that action at this time.
0 commit comments