Remove Repeating chars and Reverse String until no Repetitions
Last Updated :
19 Apr, 2023
Given a string S which consists of only lowercase English alphabets, the task is to remove the first repeating character, reverse it, and repeat until there are no repeating characters. Return the final string.
Examples:
Input: S = “abab”
Output: ba
Explanation: In 1st operation: The first non repeating character is a. After Removing the first character, S = “bab”. After Reversing the string, S = “bab”.
In 2nd operation: The first non repeating character is b. After Removing the first character, S = “ab”. After Reversing the string, S = “ba”. Now the string S does not contain any repeating character.
Input: S = “dddd”
Output: d
Approach: To solve the problem follow the below idea:
- The first repeating character must be eliminated, and then the string must be turned around. Hence, the first action is performed from the front side of the string, and the second operation is performed from the rear side of the string.
- We will use two pointer approach. Iterate the string and for each character, check if the character has not been encountered already, move the pointer forward, else reverse the pointers and repeat the process.
Follow the steps to solve the problem:
- Initialize a frequency array freq to keep track of the frequency of each character in the input string.
- Initialize the left and right pointers to the start and end indices of the input string, respectively.
- l = 0, r = s.length() – 1
- Initialize a flag f to 0.
- Iterate over the string while l ≤ r,
- If f = 0, check if the frequency count of the character at the current pointer is equal to 1, move the left pointer, else decrement the frequency count by 1, and replace the character at position l with a ‘#’, increment l and change the value of flag f by using the XOR operator.
- If the frequency count is equal to 1, move the pointer without changing direction.
- If f = 1, check if the frequency count of the character at the current pointer is equal to 1, move the right pointer, else decrement r by 1, and replace the character at position r with a ‘#’ and change the value of the flag.
- Check if f = 0. reverse the string.
- Iterate over the input string and append all alphabetic characters to a new output string ans.
- Return string ans.
Below is the code implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string removeReverse(string s)
{
int freq[26] = { 0 };
for ( int i = 0; i < s.size(); i++) {
char ch = s[i];
freq[ch - 'a' ]++;
}
int left = 0;
int right = s.length() - 1;
int flag = 0;
while (left <= right) {
if (flag == 0) {
char ch = s[left];
if (freq[ch - 'a' ] == 1) {
left++;
}
else {
freq[ch - 'a' ]--;
s.replace(left, 1, "#" );
left++;
flag ^= 1;
}
}
else {
char ch = s[right];
if (freq[ch - 'a' ] == 1) {
right--;
}
else {
freq[ch - 'a' ]--;
s.replace(right, 1, "#" );
right--;
flag ^= 1;
}
}
}
if (flag == 1)
reverse(s.begin(), s.end());
string ans = "" ;
for ( int i = 0; i < s.length(); i++) {
if (s[i] != '#' )
ans += s[i];
}
return ans;
}
int main()
{
string input = "abab" ;
string output = removeReverse(input);
cout << output << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static String removeReverse(String S)
{
StringBuffer s = new StringBuffer(S);
int freq[] = new int [ 26 ];
Arrays.fill(freq, 0 );
for ( int i = 0 ; i < s.length(); i++) {
char ch = s.charAt(i);
freq[ch - 'a' ]++;
}
int l = 0 , r = s.length() - 1 , f = 0 ;
while (l <= r) {
if (f == 0 ) {
char ch = s.charAt(l);
if (freq[ch - 'a' ] == 1 ) {
l++;
}
else {
freq[ch - 'a' ]--;
s.replace(l, l + 1 , "#" );
l++;
f ^= 1 ;
}
}
else {
char ch = s.charAt(r);
if (freq[ch - 'a' ] == 1 ) {
r--;
}
else {
freq[ch - 'a' ]--;
s.replace(r, r + 1 , "#" );
r--;
f ^= 1 ;
}
}
}
if (f == 1 )
s.reverse();
String ans = "" ;
for ( int i = 0 ; i < s.length(); i++) {
if (s.charAt(i) != '#' )
ans += s.charAt(i);
}
return ans;
}
public static void main(String[] args)
{
String input = "abab" ;
String output = removeReverse(input);
System.out.println(output);
}
}
|
Python3
def removeReverse(s):
freq = [ 0 ] * 26
for ch in s:
freq[ ord (ch) - ord ( 'a' )] + = 1
s = list (s)
l, r, f = 0 , len (s) - 1 , 0
while l < = r:
if f = = 0 :
ch = s[l]
if freq[ ord (ch) - ord ( 'a' )] = = 1 :
l + = 1
else :
freq[ ord (ch) - ord ( 'a' )] - = 1
s[l] = '#'
l + = 1
f ^ = 1
else :
ch = s[r]
if freq[ ord (ch) - ord ( 'a' )] = = 1 :
r - = 1
else :
freq[ ord (ch) - ord ( 'a' )] - = 1
s[r] = '#'
r - = 1
f ^ = 1
if f = = 1 :
s.reverse()
ans = ''
for ch in s:
if ch ! = '#' :
ans + = ch
return ans
input = 'abab'
output = removeReverse( input )
print (output)
|
C#
using System;
using System.Linq;
using System.Text;
class Program {
static string removeReverse( string S)
{
StringBuilder s = new StringBuilder(S);
int [] freq = new int [26];
for ( int i = 0; i < 26; i++) {
freq[i] = 0;
}
for ( int i = 0; i < s.Length; i++) {
char ch = s[i];
freq[ch - 'a' ]++;
}
int l = 0, r = s.Length - 1, f = 0;
while (l <= r) {
if (f == 0) {
char ch = s[l];
if (freq[ch - 'a' ] == 1) {
l++;
}
else {
freq[ch - 'a' ]--;
s[l] = '#' ;
l++;
f ^= 1;
}
}
else {
char ch = s[r];
if (freq[ch - 'a' ] == 1) {
r--;
}
else {
freq[ch - 'a' ]--;
s[r] = '#' ;
r--;
f ^= 1;
}
}
}
if (f == 1)
s.ToString().Reverse();
string ans = "" ;
for ( int i = 0; i < s.Length; i++) {
if (s[i] != '#' )
ans += s[i];
}
return ans;
}
static void Main( string [] args)
{
string input = "abab" ;
string output = removeReverse(input);
Console.WriteLine(output);
}
}
|
Javascript
function removeReverse(S) {
let s = S.split( "" );
let freq = new Array(26).fill(0);
for (let i = 0; i < s.length; i++) {
let ch = s[i];
freq[ch.charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
let l = 0, r = s.length - 1, f = 0;
while (l <= r) {
if (f === 0) {
let ch = s[l];
if (freq[ch.charCodeAt(0) - 'a' .charCodeAt(0)] === 1) {
l++;
}
else {
freq[ch.charCodeAt(0) - 'a' .charCodeAt(0)]--;
s[l] = "#" ;
l++;
f ^= 1;
}
}
else {
let ch = s[r];
if (freq[ch.charCodeAt(0) - 'a' .charCodeAt(0)] === 1) {
r--;
}
else {
freq[ch.charCodeAt(0) - 'a' .charCodeAt(0)]--;
s[r] = "#" ;
r--;
f ^= 1;
}
}
}
if (f === 1)
s.reverse();
let ans = "" ;
for (let i = 0; i < s.length; i++) {
if (s[i] !== '#' )
ans += s[i];
}
return ans;
}
let input = "abab" ;
let output = removeReverse(input);
console.log(output);
|
Time Complexity: O(N), where N is the length of the string
Auxiliary Space: O(K), K ≤ 26.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...