Skip to content

Commit 752d882

Browse files
committed
solve 387.first-unique-character-in-a-string
1 parent 7ea359b commit 752d882

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode id=387 lang=java
3+
*
4+
* [387] First Unique Character in a String
5+
*
6+
* https://leetcode.com/problems/first-unique-character-in-a-string/description/
7+
*
8+
* algorithms
9+
* Easy (49.20%)
10+
* Total Accepted: 253.6K
11+
* Total Submissions: 510.2K
12+
* Testcase Example: '"leetcode"'
13+
*
14+
*
15+
* Given a string, find the first non-repeating character in it and return it's
16+
* index. If it doesn't exist, return -1.
17+
*
18+
* Examples:
19+
*
20+
* s = "leetcode"
21+
* return 0.
22+
*
23+
* s = "loveleetcode",
24+
* return 2.
25+
*
26+
*
27+
*
28+
*
29+
* Note: You may assume the string contain only lowercase letters.
30+
*
31+
*/
32+
class Solution {
33+
public int firstUniqChar(String s) {
34+
int len = s.length();
35+
int[] count = new int[26];
36+
for (int i = 0; i < len; i++) {
37+
count[s.charAt(i) - 'a']++;
38+
}
39+
for (int i = 0; i < len; i++) {
40+
if (count[s.charAt(i) - 'a'] == 1) {
41+
return i;
42+
}
43+
}
44+
return -1;
45+
}
46+
}
47+

0 commit comments

Comments
 (0)