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 character in first string that is present at minimum index in second string
- Queries to find the first non-repeating character in the sub-string of a string
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Create new linked list from two given linked list with greater element at each node
- Efficiently find first repeated character in a string without using any additional data structure in one traversal
- Given a string, find its first non-repeating character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace all occurrences of character X with character Y in given string
- Find the product of first k nodes of the given Linked List
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Merge a linked list into another linked list at alternate positions
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- First non-repeating character using one traversal of string | Set 2
- Find the first repeated character in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : rutvik_56