DSA Day 18/100

DSA Day 18/100

Topic: String

Questions Successfully Completed: 1

1) Non-Repeating Character

Easy

Non Repeating Character

Time Complexity : O(n)

Space Complexity : O(n)

Question
Input: S = hello Output: h Explanation: In the given string, the first non-repeating character is h, as it appears first and there is no other 'h' in the string.
static char nonrepeatingCharacter(String S)
    {
        // hash table with 26 indexes
        int[] str_hash = new int[26];

        char[] char_hash = S.toCharArray();
        char ch = '\0';

        for(int i=0;i<char_hash.length;i++){
            str_hash[char_hash[i]-97]++;
        }
        for (int i=0;i<char_hash.length;i++){
            if((str_hash[char_hash[i]-97])==1){
                ch = char_hash[i];
                return ch;
            }
        }
        return '$';
    }

Thank you for reading :)