-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstuniqueChar.java
More file actions
32 lines (29 loc) · 820 Bytes
/
Copy pathFirstuniqueChar.java
File metadata and controls
32 lines (29 loc) · 820 Bytes
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
class Solution {
public int firstUniqChar(String s) {
// int count[] = new int[26] ;
// for(int i=0;i<s.length();i++)
// {
// int index = s.charAt(i)-'a';
// count[index]++;
// }
// for(int i=0;i<s.length();i++)
// {
// int index = s.charAt(i)-'a';
// if(count[index] == 1)
// {
// return i;
// }
// }
// return -1;
int answer = Integer.MAX_VALUE;
for(char c = 'a';c<='z';++c)
{
int index = s.indexOf(c);
if(index!=-1 && index == s.lastIndexOf(c))
{
answer = Math.min(answer,index);
}
}
return answer == Integer.MAX_VALUE?-1:answer;
}
}