-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
41 lines (38 loc) · 1.11 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package firstuniquecharacterinastring;
/**
* Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
* <p>
* Examples:
* <p>
* s = "leetcode"
* return 0.
* <p>
* s = "loveleetcode",
* return 2.
* Note: You may assume the string contain only lowercase letters.
*/
class Solution {
public int firstUniqChar(String s) {
// lowercase letters a: 97, z: 122, diff: 0->25(26 letters)
int[] asciiCodes = new int[26];
int idx = -1;
for (int i = 0; i < s.length(); i++) {
// offset to fit within 0-25
idx = ((int) s.charAt(i)) - 97;
asciiCodes[idx]++;
}
for (int i = 0; i < s.length(); i++) {
idx = ((int) s.charAt(i)) - 97;
if (asciiCodes[idx] == 1) {
return i;
}
}
return -1;
}
}
/*
Success
Details
Runtime: 8 ms, faster than 84.32% of Java online submissions for First Unique Character in a String.
Memory Usage: 41.8 MB, less than 5.71% of Java online submissions for First Unique Character in a String.
*/