Find the last non repeating character in string | C ++ implementation of the approach ; Maximum distinct characters possible ; Function to return the last non - repeating character ; To store the frequency of each of the character of the given string ; Update the frequencies ; Starting from the last character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the string are repeating ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 256 ; static string lastNonRepeating ( string str , int n ) { int freq [ MAX ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) freq [ str . at ( i ) ] ++ ; for ( int i = n - 1 ; i >= 0 ; i -- ) { char ch = str . at ( i ) ; if ( freq [ ch ] == 1 ) { string res ; res += ch ; return res ; } } return " - 1" ; } int main ( ) { string str = " GeeksForGeeks " ; int n = str . size ( ) ; cout << lastNonRepeating ( str , n ) ; return 0 ; }