Given a string str of length L, the task is to find the first occurrence of a non-repeating character in the string.
Input: str = “geeksforgeeks”
Input: str = “programmer”
Linked List Approach: The idea is to use Linked List to keep track of the unique elements in the string. Below is the illustration of the approach:
- Iterate over the string for each character in the string and add the character in the Linked List on basis of the below conditions:
- If the character is already present in the Linked List, then remove the existing character node from the linked list.
- Otherwise, Add the character into the linked list.
- Finally, the character at the first node of the linked list is the first non-repeating character of the string.
Below is the implementation of the above approach:
- Time Complexity: O(N * 26)
- Auxiliary Space: 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.
- Find the string formed by joining k consecutive nodes of linked list
- Maximum occurring character in a linked list
- Find a string such that every character is lexicographically greater than its immediate next character
- Find the character in first string that is present at minimum index in second string
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Find the last non repeating character in string
- Find k-th character of decrypted string | Set - 2
- Find k'th character of decrypted string | Set 1
- Find last index of a character in a string
- Find the first repeated character in a string
- Find one extra character in a string
- Given a string, find its first non-repeating character
- How to find the first and last character of a string in Java
- Find the Nth occurrence of a character in the given String
- Convert a String to a Singly Linked List
- Program to find the kth character after decrypting a string
- Find repeated character present first in a string
- Swap the vowels in the linked list representation of a string
- Reverse all the word in a String represented as a Linked List
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.