package com.hiwhitley.easy.day17;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by hiwhitley on 2016/9/11.
 */
public class FirstUniqChar {

    public static int firstUniqChar(String s) {
        LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
        for (int i = 0; i < s.length(); i++) {
            Integer t = map.get(s.charAt(i));
            if (t == null) {
                map.put(s.charAt(i), 1);
            } else {
                map.put(s.charAt(i), t + 1);
            }
        }
        for (Map.Entry<Character,Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                return s.indexOf(entry.getKey());
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(firstUniqChar("cdcacb"));
    }
}
