Skip to content

Commit 2a71961

Browse files
committed
Java solution 387
1 parent 99be045 commit 2a71961

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
*
3+
* Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
4+
*
5+
* Examples:
6+
*
7+
* s = "leetcode"
8+
* return 0.
9+
*
10+
* s = "loveleetcode",
11+
* return 2.
12+
* Note: You may assume the string contain only lowercase letters.
13+
*/
14+
public class _387FirstUniqueCharacterinaString {
15+
public int firstUniqChar(String s) {
16+
int[] frequency = new int[26];
17+
for (int i = 0; i < s.length(); i++) {
18+
frequency[s.charAt(i) - 'a']++;
19+
}
20+
for (int i = 0; i < s.length(); i++) {
21+
if (frequency[s.charAt(i) - 'a'] == 1) {
22+
return i;
23+
}
24+
}
25+
return -1;
26+
}
27+
}

0 commit comments

Comments
 (0)