Given a string, find the first non-repeating character in it. For example, if the input string is “GeeksforGeeks”, then output should be ‘f’ and if input string is “GeeksQuiz”, then output should be ‘G’.
We have discussed two solutions in Given a string, find its first non-repeating character . In this post a further optimized solution (over method 2 of previous post) is discussed. The idea is to optimize space. Instead of using a pair to store count and index, we use single element that store index if element appears once, else stores a negative value.
First non-repeating character is f
Time Complexity: O(n)
This is coded using a HashMap or Hashing Technique.
If the element(or key) repeats in the string the HashMap (or Dictionary) will change the value of that key to None.
This way we will later on only be finding keys whose value is “not None”.
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Efficiently find first repeated character in a string without using any additional data structure in one traversal
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Count of substrings having the most frequent character in the string as first character
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Sub-strings that start and end with one character and have at least one other
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Convert the string into palindrome string by changing only one character
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Find the character in first string that is present at minimum index in second string
- Find first non-repeating character in a given string using Linked List
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace all occurrences of character X with character Y in given string
- Longest Common Prefix using Character by Character Matching
- Check if both halves of the string have at least one different character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.