Eliminar todas las apariciones continuas de ‘a’ y todas las apariciones de ‘b’

Dada una string str , la tarea es eliminar todas las ocurrencias continuas de a y todas las ocurrencias de b e imprimir la string resultante.
Ejemplos: 
 

Entrada: str = “abcddabcddddabbbaaaaaa” 
Salida: acddacdddda 
‘abcddabcddddabbbaaaaaa’ no dará como resultado ‘acddacddddaa’ porque después de eliminar las ocurrencias requeridas, la string se convertirá en ‘acddacddddaa’, lo que dará como resultado ‘acddacdddda’
Entrada: str = “aacbccdbsssaba” 
Salida: accdsssa 
 

Enfoque: Inicializamos una string de resultado vacía. Atravesamos la string de entrada si el carácter actual es b o el carácter actual es a y el último carácter de la string de resultado también es a , luego ignoramos el carácter; de lo contrario, insertamos el carácter en la string de resultado.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to get the resultant string after
// removing the required occurrences
string removeOccurrences(string str)
{
 
    // String to store the resultant string
    string res = "";
    for (int i = 0; i < str.size(); i++) {
 
        // If 'a' appeared more than once continuously
        if (str[i] == 'a' && res.back() == 'a')
 
            // Ignore the character
            continue;
 
        // Ignore all 'b' characters
        else if (str[i] == 'b')
            continue;
 
        // Characters that will be included
        // in the resultant string
        res = res + str[i];
    }
    return res;
}
 
// Driver code
int main()
{
    string str = "abcddabcddddabbbaaaaaa";
    cout << removeOccurrences(str);
    return 0;
}

Java

//Java implementation of the approach
class solution
{
// Function to get the resultant String after
// removing the required occurrences
static String removeOccurrences(String str)
{
 
    // String to store the resultant String
    String res = "";
    for (int i = 0; i < str.length(); i++) {
 
        // If 'a' appeared more than once continuously
        if (str.charAt(i) == 'a' && (res.length()==0?' ':res.charAt(res.length()-1)) == 'a')
 
            // Ignore the character
            continue;
 
        // Ignore all 'b' characters
        else if (str.charAt(i) == 'b')
            continue;
 
        // Characters that will be included
        // in the resultant String
        res = res + str.charAt(i);
    }
    return res;
}
 
// Driver code
public static void main(String args[])
{
    String str = "abcddabcddddabbbaaaaaa";
    System.out.println(removeOccurrences(str));
}
}
//contributed by Arnab Kundu

Python3

# Python3 implementation of the approach
 
# Function to get the resultant string
# after removing the required occurrences
def removeOccurrences(str) :
 
    # String to store the resultant string
    res = ""
    for i in range(len(str)) :
         
        # If 'a' appeared more than
        # once continuously
        if (res) :
             
            if (str[i] == 'a' and res[-1] == 'a') :
 
                # Ignore the character
                continue
             
            # Ignore all 'b' characters
            elif (str[i] == 'b') :
                continue
             
            else :
                # Characters that will be included
                # in the resultant string
                res += str[i]
         
        else :
             
            if (str[i] == 'a' ) :
                res += str[i]
                 
            # Ignore all 'b' characters
            elif (str[i] == 'b') :
                continue
             
            else :
                # Characters that will be included
                # in the resultant string
                res += str[i]
 
    return res
 
# Driver code
if __name__ == "__main__" :
 
    str = "abcddabcddddabbbaaaaaa"
    print(removeOccurrences(str))
     
# This code is contributed by Ryuga

C#

// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to get the resultant String after
// removing the required occurrences
static String removeOccurrences(String str)
{
 
    // String to store the resultant String
    String res = "";
    for (int i = 0; i < str.Length; i++)
    {
 
        // If 'a' appeared more than once continuously
        if (str[i] == 'a' && (res.Length==0?' ':
                        res[res.Length-1]) == 'a')
 
            // Ignore the character
            continue;
 
        // Ignore all 'b' characters
        else if (str[i] == 'b')
            continue;
 
        // Characters that will be included
        // in the resultant String
        res = res + str[i];
    }
    return res;
}
 
// Driver code
public static void Main(String []args)
{
    String str = "abcddabcddddabbbaaaaaa";
    Console.WriteLine(removeOccurrences(str));
}
}
 
// This code has been contributed by 29AjayKumar

Javascript

<script>
 
// JavaScript implementation of the approach
 
// Function to get the resultant String after
// removing the required occurrences
function removeOccurrences(str)
{
 
    // String to store the resultant String
    var res = "";
    for (var i = 0; i < str.length; i++) {
 
        // If 'a' appeared more than once continuously
        if (str.charAt(i) == 'a' &&
        (res.length==0?' ':res.charAt(res.length-1)) == 'a')
 
            // Ignore the character
            continue;
 
        // Ignore all 'b' characters
        else if (str.charAt(i) == 'b')
            continue;
 
        // Characters that will be included
        // in the resultant String
        res = res + str.charAt(i);
    }
    return res;
}
 
// Driver code
var str = "abcddabcddddabbbaaaaaa";
document.write(removeOccurrences(str));
 
 
// This code contributed by Princi Singh
 
</script>
Producción: 

acddacdddda

 

Publicación traducida automáticamente

Artículo escrito por coder_11 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *