Given a string and a number k, find the k’th non-repeating character in the string. Consider a large input string with lacs of characters and a small character set. How to find the character by only doing only one traversal of input string?
Input : str = geeksforgeeks, k = 3 Output : r First non-repeating character is f, second is o and third is r. Input : str = geeksforgeeks, k = 2 Output : o Input : str = geeksforgeeks, k = 4 Output : Less than k non-repeating characters in input.
This problem is mainly an extension of First non-repeating character problem.
Method 1 (Simple : O(n2)
A Simple Solution is to run two loops. Start traversing from left side. For every character, check if it repeats or not. If the character doesn’t repeat, increment count of non-repeating characters. When the count becomes k, return the character.
Method 2 (O(n) but requires two traversals)
- Create an empty hash.
- Scan input string from left to right and insert values and their counts in the hash.
- Scan input string from left to right and keep count of characters with counts more than 1. When count becomes k, return the character.
Method 3 (O(n) and requires one traversal)
The idea is to use two auxiliary arrays of size 256 (Assuming that characters are stored using 8 bits). The two arrays are:
count[x] : Stores count of character 'x' in str. If x is not present, then it stores 0. index[x] : Stores indexes of non-repeating characters in str. If a character 'x' is not present or x is repeating, then it stores a value that cannot be a valid index in str. For example, length of string.
- Initialize all values in count as 0 and all values in index as n where n is length of string.
- Traverse the input string str and do following for every character c = str[i].
- Increment count[x].
- If count[x] is 1, then store index of x in index[x], i.e., index[x] = i
- If count[x] is 2, then remove x from index, i.e., index[x] = n
- Now index has indexes of all non-repeating characters. Sort index in increasing order so that we get k’th smallest element at index[k]. Note that this step takes O(1) time because there are only 256 elements in index.
Below is implementation of above idea.
k'th non-repeating character is r
Space Optimized Solution :
This can be space optimized and can be solved using single index array only. Below is the space optimized solution:
k'th non-repeating character is r
This article is contributed by Shivam Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Longest Common Prefix using Character by Character Matching
- Find a string such that every character is lexicographically greater than its immediate next character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace every character of string by character whose ASCII value is K times more than it
- Count substrings that starts with character X and ends with character Y
- Map every character of one string to another such that all occurrences are mapped to the same character
- Shortest distance to every other character from given character
- Replace every character of a string by a different character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Replace a character c1 with c2 and c2 with c1 in a string S
- Check if all occurrences of a character appear together
- Sub-strings of length K containing same character
- Sub-strings that start and end with one character and have at least one other
- Palindrome by swapping only one character
- Count of sub-strings that contain character X at least once