Given a string, find its first unique character and return its index. If it does not exist, -1 is returned.
Example:
s = "leetcode"
returns 0
s = "loveleetcode"
returns 2
Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/first-unique-character-in-a-string
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
class Solution {
public int firstUniqChar(String s) {
int[] mark = new int[26];
for(int i=0;i<s.length();i++)
{
char ch = s.charAt(i);
mark[ch-'a']++;
}
for(int i=0;i<s.length();i++)
{
char ch = s.charAt(i);
if(mark[ch-'a']==1)
{
return i;
}
}
return -1;
}
}