Find the last non repeating character in string
Last Updated :
15 Mar, 2023
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 the input string is “GeeksQuiz” then the output should be ‘z’. if there is no non-repeating character then print -1.
Examples:
Input: str = “GeeksForGeeks”
Output: r
‘r’ is the first character from the end which has frequency 1.
Input: str = “aabbcc”
Output: -1
All the characters of the given string have frequencies greater than 1.
Approach: Create a frequency array that will store the frequency of each of the characters 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 a frequency greater than 1 then print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
public class GFG {
static final int MAX = 256 ;
static String lastNonRepeating(String str, int n)
{
int freq[] = new int [MAX];
for ( int i = 0 ; i < n; i++)
freq[str.charAt(i)]++;
for ( int i = n - 1 ; i >= 0 ; i--) {
char ch = str.charAt(i);
if (freq[ch] == 1 )
return ( "" + ch);
}
return "-1" ;
}
public static void main(String[] args)
{
String str = "GeeksForGeeks" ;
int n = str.length();
System.out.println(lastNonRepeating(str, n));
}
}
|
Python3
MAX = 256 ;
def lastNonRepeating(string, n) :
freq = [ 0 ] * MAX ;
for i in range (n) :
freq[ ord (string[i])] + = 1 ;
for i in range (n - 1 , - 1 , - 1 ) :
ch = string[i];
if (freq[ ord (ch)] = = 1 ) :
return ("" + ch);
return "-1" ;
if __name__ = = "__main__" :
string = "GeeksForGeeks" ;
n = len (string);
print (lastNonRepeating(string, n));
|
C#
using System;
class GFG
{
static readonly int MAX = 256;
static String lastNonRepeating(String str, int n)
{
int []freq = new int [MAX];
for ( int i = 0; i < n; i++)
freq[str[i]]++;
for ( int i = n - 1; i >= 0; i--)
{
char ch = str[i];
if (freq[ch] == 1)
return ( "" + ch);
}
return "-1" ;
}
public static void Main(String[] args)
{
String str = "GeeksForGeeks" ;
int n = str.Length;
Console.WriteLine(lastNonRepeating(str, n));
}
}
|
Javascript
function lastNonRepeating(str, n) {
const MAX = 256;
const freq = new Array(MAX).fill(0);
for (let i = 0; i < n; i++)
freq[str.charCodeAt(i)]++;
for (let i = n - 1; i >= 0; i--) {
const ch = str.charAt(i);
if (freq[ch.charCodeAt()] == 1) return ch;
}
return "-1" ;
}
const str = "GeeksForGeeks" ;
const n = str.length;
console.log(lastNonRepeating(str, n));
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(256) ? O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...