Subsecuencia más larga tal que no hay 3 caracteres consecutivos iguales

Dada una string de caracteres S en minúsculas , la tarea es encontrar la subsecuencia más larga de la string sin 3 caracteres idénticos consecutivos.
Ejemplos :

Entrada: S = “eedaaad”
Salida: eedaad
Explicación: Se elimina una aparición de la letra a.

Entrada: xxxtxxx
Salida: xxtxx

 

Enfoque : la tarea se puede resolver comprobando cada ventana de tamaño 3. Si alguno de los 3 caracteres no coincide, agréguelo a la string resultante; de ​​lo contrario, continúe. Por último, imprima la string resultante.

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the
// longest subsequence
string filterString(string s1)
{
    string sb1 = "";
 
    // Append the first character
    sb1 += s1[0];
 
    // Append the second character
    sb1 += (s1[1]);
 
    // Loop for i=2 to n
    for (int i = 2; i < s1.length(); ++i)
    {
 
        // If consecutive three element
        // are not equal then append
        if (s1[i] != s1[i - 1] || s1[i] != s1[i - 2])
        {
            sb1 += s1[i];
        }
    }
    return sb1;
}
 
// Driver Code
int main()
{
    string s = "eedaaad";
    string res = filterString(s);
    cout << (res);
    return 0;
}
// This code is contributed by Potta Lokesh

Java

// Java program for the above approach
class Solution {
 
    // Function to find the
    // longest subsequence
    public static String
    filterString(String s1)
    {
        StringBuilder sb1 = new StringBuilder();
 
        // Append the first character
        sb1.append(s1.charAt(0));
 
        // Append the second character
        sb1.append(s1.charAt(1));
 
        // Loop for i=2 to n
        for (int i = 2; i < s1.length();
             ++i) {
 
            // If consecutive three element
            // are not equal then append
            if (s1.charAt(i) != s1.charAt(i - 1)
                || s1.charAt(i) != s1.charAt(i - 2)) {
                sb1.append(s1.charAt(i));
            }
        }
        return sb1.toString();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String s = "eedaaad";
        String res = filterString(s);
        System.out.println(res);
    }
}

Python3

# Python 3 code for the above approach
 
# Function to find the
# longest subsequence
def filterString(s1):
 
    sb1 = ""
 
    # Append the first character
    sb1 += s1[0]
 
    # Append the second character
    sb1 += (s1[1])
 
    # Loop for i=2 to n
    for i in range(2, len(s1)):
 
        # If consecutive three element
        # are not equal then append
        if (s1[i] != s1[i - 1] or s1[i] != s1[i - 2]):
 
            sb1 += s1[i]
 
    return sb1
 
# Driver Code
if __name__ == "__main__":
 
    s = "eedaaad"
    res = filterString(s)
    print(res)
 
    # This code is contributed by ukasp.

C#

// C# program for the above approach
using System;
using System.Text;
class Solution
{
 
    // Function to find the
    // longest subsequence
    public static string filterstring(string s1)
    {
        StringBuilder sb1 = new StringBuilder();
 
        // Append the first character
        sb1.Append(s1[0]);
 
        // Append the second character
        sb1.Append(s1[1]);
 
        // Loop for i=2 to n
        for (int i = 2; i < s1.Length; ++i)
        {
 
            // If consecutive three element
            // are not equal then append
            if (s1[i] != s1[i - 1]
                || s1[i] != s1[i - 2])
            {
                sb1.Append(s1[i]);
            }
        }
        return sb1.ToString();
    }
 
    // Driver Code
    public static void Main()
    {
        string s = "eedaaad";
        string res = filterstring(s);
        Console.Write(res);
    }
}
 
// This code is contributed by gfgking.

Javascript

<script>
    // JavaScript code for the above approach
    // Function to find the
    // longest subsequence
    const filterString = (s1) => {
        let sb1 = "";
 
        // Append the first character
        sb1 += s1[0];
 
        // Append the second character
        sb1 += (s1[1]);
 
        // Loop for i=2 to n
        for (let i = 2; i < s1.length; ++i) {
 
            // If consecutive three element
            // are not equal then append
            if (s1[i] != s1[i - 1] || s1[i] != s1[i - 2]) {
                sb1 += s1[i];
            }
        }
        return sb1;
    }
 
    // Driver Code
    let s = "eedaaad";
    let res = filterString(s);
    document.write(res);
 
    // This code is contributed by rakeshsahni
 
</script>
Producción: 

eedaad

 

Complejidad de tiempo : O(N), donde N es la longitud de la string
Espacio auxiliar : O(1)

Publicación traducida automáticamente

Artículo escrito por annianni 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 *