Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode" return 0. s = "loveleetcode", return 2.
Note: You may assume the string contain only lowercase letters.
第一步给所有字母计数
第二部从头开始扫描串,检查字母对应的计数值,如果为1就找到了
public class Solution {
public int firstUniqChar(String s)
{
int len=s.length();
int[] arr=new int[128];
for(int i=0;i<len;i++)
arr[s.charAt(i)]++;
for(int i=0;i<len;i++)
if(arr[s.charAt(i)]==1)
return i;
return -1;
}
}