Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
样例
Given s = "lintcode", return 0.
样例
Given s = "lintcode", return 0.
Given s = "lovelintcode", return 2.
import java.util.Scanner;
/**
* Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
样例
Given s = "lintcode", return 0.
Given s = "lovelintcode", return 2.
*
* @author Dell
*
*/
public class Test646 {
public static int firstUniqChar(String s)
{
if(s.equals(""))
{
return 0;
}
int[] chr=new int[256];
for(int i=0;i<s.length();i++)
{
chr[s.charAt(i)]++;
}
for(int i=0;i<s.length();i++)
{
if(chr[s.charAt(i)]==1)
{
return i;
}
}
return -1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
System.out.println(firstUniqChar(s));
}
}