Remove Repeating chars and Reverse String until no Repetitions
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++
// C++ code for the approach #include <bits/stdc++.h> using namespace std; // Function to remove and reverse characters from input // string string removeReverse(string s) { // Step 1: Convert the input string to a StringBuffer // and create a frequency count array Initializing // frequency count array int freq[26] = { 0 }; // Finding frequency of each character in the input // string for ( int i = 0; i < s.size(); i++) { char ch = s[i]; freq[ch - 'a' ]++; } // Step 2: Initialize the left and right pointers, and // the direction counter int left = 0; int right = s.length() - 1; int flag = 0; // Step 3-6: Remove and reverse characters from input // string while (left <= right) { // If the counter is zero, move the left pointer, // otherwise, move the right pointer if (flag == 0) { // Check if the frequency count of the character // at the current pointer is greater than 1 char ch = s[left]; if (freq[ch - 'a' ] == 1) { // If the frequency count is equal to 1, // move the pointer without changing // direction left++; } else { // Otherwise, decrement the frequency count // by 1 and change the direction of the // pointers 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; } } } // Step 5-6: Replace non-alphabetic characters with a // placeholder and append alphabetic characters to a new // output string if (flag == 1) reverse(s.begin(), s.end()); // Removing placeholders from the StringBuffer and // creating the output string string ans = "" ; for ( int i = 0; i < s.length(); i++) { if (s[i] != '#' ) ans += s[i]; } // Step 7: If the counter variable is equal to 1, // reverse the output string before returning it return ans; } // Driver code int main() { // Taking input string from user string input = "abab" ; // Calling function to remove and reverse characters // from input string string output = removeReverse(input); // Displaying output string cout << output << endl; return 0; } |
Java
import java.util.Arrays; class GFG { static String removeReverse(String S) { // Step 1: Convert the input // string to a StringBuffer and // create a frequency count array 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' ]++; } // Step 2: Initialize the left and // right pointers, and the // direction counter int l = 0 , r = s.length() - 1 , f = 0 ; while (l <= r) { // Step 3: If the counter is // zero, move the left pointer, // otherwise, move the // right pointer if (f == 0 ) { // Step 4: Check if the // frequency count of the // character at the current // pointer isgreater than 1 char ch = s.charAt(l); if (freq[ch - 'a' ] == 1 ) { // If the frequency count // is equal to 1, move // the pointer without // changing direction l++; } else { // Otherwise, decrement // the frequency count // by 1 and change the // direction of the // pointers 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 ; } } } // Step 5: Replace non-alphabetic // characters with a placeholder // and remove them later // Step 6: Iterate over the input // string and append any alphabetic // characters to a new output string if (f == 1 ) s.reverse(); String ans = "" ; for ( int i = 0 ; i < s.length(); i++) { if (s.charAt(i) != '#' ) ans += s.charAt(i); } // Step 7: If the counter variable // is equal to 1, reverse the // output string before returning it return ans; } // Driver code public static void main(String[] args) { String input = "abab" ; String output = removeReverse(input); System.out.println(output); } } |
Python3
def removeReverse(s): # Step 1: Convert the input # string to a list and # create a frequency count array 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 # Driver code input = 'abab' output = removeReverse( input ) print (output) |
C#
using System; using System.Linq; using System.Text; class Program { static string removeReverse( string S) { // Step 1: Convert the input // string to a StringBuffer and // create a frequency count array 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' ]++; } // Step 2: Initialize the left and // right pointers, and the // direction counter int l = 0, r = s.Length - 1, f = 0; while (l <= r) { // Step 3: If the counter is // zero, move the left pointer, // otherwise, move the // right pointer if (f == 0) { // Step 4: Check if the // frequency count of the // character at the current // pointer isgreater than 1 char ch = s[l]; if (freq[ch - 'a' ] == 1) { // If the frequency count // is equal to 1, move // the pointer without // changing direction l++; } else { // Otherwise, decrement // the frequency count // by 1 and change the // direction of the // pointers 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; } } } // Step 5: Replace non-alphabetic // characters with a placeholder // and remove them later // Step 6: Iterate over the input // string and append any alphabetic // characters to a new output string if (f == 1) s.ToString().Reverse(); string ans = "" ; for ( int i = 0; i < s.Length; i++) { if (s[i] != '#' ) ans += s[i]; } // Step 7: If the counter variable // is equal to 1, reverse the // output string before returning it return ans; } // Driver code static void Main( string [] args) { string input = "abab" ; string output = removeReverse(input); Console.WriteLine(output); } } // This code is contributed by Tapesh(tapeshdua420) |
Javascript
function removeReverse(S) { // Step 1: Convert the input string to a StringBuffer and // create a frequency count array 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)]++; } // Step 2: Initialize the left and right pointers, and the direction counter let l = 0, r = s.length - 1, f = 0; while (l <= r) { // Step 3: If the counter is zero, move the left pointer, // otherwise, move the right pointer if (f === 0) { // Step 4: Check if the frequency count of the character at // the current pointer is greater than 1 let ch = s[l]; if (freq[ch.charCodeAt(0) - 'a' .charCodeAt(0)] === 1) { // If the frequency count is equal to 1, move the pointer // without changing direction l++; } else { // Otherwise, decrement the frequency count by 1 and change // the direction of the pointers 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; } } } // Step 5: Replace non-alphabetic characters with a placeholder and // remove them later //Step 6: Iterate over the input string and append any alphabetic // characters to a new output string if (f === 1) s.reverse(); let ans = "" ; for (let i = 0; i < s.length; i++) { if (s[i] !== '#' ) ans += s[i]; } // Step 7: If the counter variable is equal to 1, // reverse the output string before returning it return ans; } // Driver code let input = "abab" ; let output = removeReverse(input); console.log(output); //This code is contributed by Tushar Rokade |
ba
Time Complexity: O(N), where N is the length of the string
Auxiliary Space: O(K), K ≤ 26.
Please Login to comment...