Given a string str, the task is to find the last non-repeating character in it.
For example, if the input string is “GeeksForGeeks”, then the output should be ‘r’ and if input string is “GeeksQuiz” then the output should be ‘z’. if there is no non-repeating character then print -1.
Input: str = “GeeksForGeeks”
‘r’ is the first character from the end which has frequency 1.
Input: str = “aabbcc”
All the characters of the given string have frequencies greater than 1.
Approach: Create a frequency array which will store the frequency of each of the character of the given string. Once the frequencies have been updated, start traversing the string from the end character by character and for every character, if the frequency of the current character is 1 then this is the last non-repeating character. If all the characters have frequency greater than 1 then print -1.
Below is the implementation of the above approach:
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
- Given a string, find its first non-repeating character
- Queries to find the first non-repeating character in the sub-string of a string
- Queries to find the last non-repeating character in the sub-string of a given string
- First non-repeating character using one traversal of string | Set 2
- Encrypt a string by repeating i-th character i times
- Maximum consecutive repeating character in string
- Decrypt a string encrypted by repeating i-th character i times
- Find the first non-repeating character from a stream of characters
- 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
- K'th Non-repeating Character
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- Queue based approach for first non-repeating character in a stream
- Find the Nth occurrence of a character in the given String
- Find k'th character of decrypted string | Set 1
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.