First Position Unique Character

来源:互联网 发布:mac 网络拓扑图软件 编辑:程序博客网 时间:2024/06/10 14:13

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.


public int firstUniqChar(String s) {        int[] map = new int[200];        for (int i = 0; i < s.length(); i++) {            char c = s.charAt(i);            map[c]++;        }        for (int i = 0; i < s.length(); i++) {            char c = s.charAt(i);            if (map[c] == 1) return i;        }        return -1;    }