17: Find the first non-repeating character in a string.
⏳⌚ 00:00:00

Question:-

Find the first non-repeating character in a string.
Example 1:
Input: geeksforgeeks
Output: f
Example 2:
Input: aabbcc
Output: No non-repeating character found.

Steps to solve:-

1. firstNonRepeatingCharacter Function:-
It defines a function that takes a string str as input and returns a character.
Inside this function, it uses an unordered_map named charCount to store the count of each character in the input string.
The first for loop iterates through the characters of the input string, and for each character encountered, it increments its count in the charCount map.
The second for loop also iterates through the characters of the input string. It checks the count of each character, and when it finds a character with a count of 1 (i.e., a non-repeating character), it returns that character.
If no non-repeating character is found, it returns '\0' as a default value.
2. main function:- In the main function, the program reads a string input from the user using cin.
It then calls the firstNonRepeatingCharacter function with the input string and stores the result in the result variable.
It checks whether result is equal to '\0' to determine if a non-repeating character was found or not.
If a non-repeating character is found, it prints that character. Otherwise, it prints a message indicating that no non-repeating character was found.

solution->

View Code 1
Time Complexity = O(n)
Space Complexity = O(n)