Find the first un-repeated character in a string

后端 未结 30 1404
猫巷女王i
猫巷女王i 2020-11-27 18:29

What is the quickest way to find the first character which only appears once in a string?

30条回答
  •  无人及你
    2020-11-27 19:09

    I have two strings i.e. 'unique' and 'repeated'. Every character appearing for the first time, gets added to 'unique'. If it is repeated for the second time, it gets removed from 'unique' and added to 'repeated'. This way, we will always have a string of unique characters in 'unique'. Complexity big O(n)

    public void firstUniqueChar(String str){
        String unique= "";
        String repeated = "";
        str = str.toLowerCase();
        for(int i=0; i

提交回复
热议问题