La subsecuencia lexicográficamente más grande de la string dada

Dada una string str que contiene caracteres en minúsculas, la tarea es encontrar la subsecuencia lexicográficamente más grande de str .
Ejemplos: 
 

Entrada: str = “abc” 
Salida:
Todas las subsecuencias posibles son “a”, “ab”, “ac”, “b”, “bc” y “c” 
y “c” es la más grande entre ellas (lexicográficamente )
Entrada: str = «geeksforgeeks» 
Salida: ss 
 

Enfoque: Sea mx el carácter lexicográficamente más grande de la string. Dado que queremos la subsecuencia lexicográficamente más grande, debemos incluir todas las apariciones de mx . Ahora, después de que se hayan utilizado todas las ocurrencias, se puede repetir el mismo proceso para la string restante (es decir, la substring después de la última ocurrencia de mx ) y así sucesivamente hasta que no queden más caracteres.
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 return the lexicographically
// largest sub-sequence of s
string getSubSeq(string s, int n)
{
    string res = "";
    int cr = 0;
    while (cr < n) {
 
        // Get the max character from the string
        char mx = s[cr];
        for (int i = cr + 1; i < n; i++)
            mx = max(mx, s[i]);
        int lst = cr;
 
        // Use all the occurrences of the
        // current maximum character
        for (int i = cr; i < n; i++)
            if (s[i] == mx) {
                res += s[i];
                lst = i;
            }
 
        // Repeat the steps for the remaining string
        cr = lst + 1;
    }
    return res;
}
 
// Driver code
int main()
{
    string s = "geeksforgeeks";
    int n = s.length();
    cout << getSubSeq(s, n);
}

Java

// Java implementation of the approach
class GFG
{
 
    // Function to return the lexicographically
    // largest sub-sequence of s
    static String getSubSeq(String s, int n)
    {
        String res = "";
        int cr = 0;
        while (cr < n)
        {
 
            // Get the max character from the String
            char mx = s.charAt(cr);
            for (int i = cr + 1; i < n; i++)
            {
                mx = (char) Math.max(mx, s.charAt(i));
            }
            int lst = cr;
 
            // Use all the occurrences of the
            // current maximum character
            for (int i = cr; i < n; i++)
            {
                if (s.charAt(i) == mx)
                {
                    res += s.charAt(i);
                    lst = i;
                }
            }
 
            // Repeat the steps for
            // the remaining String
            cr = lst + 1;
        }
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "geeksforgeeks";
        int n = s.length();
        System.out.println(getSubSeq(s, n));
    }
}
 
// This code is contributed by Rajput-Ji

Python3

# Python 3 implementation of the approach
 
# Function to return the lexicographically
# largest sub-sequence of s
def getSubSeq(s, n):
    res = ""
    cr = 0
    while (cr < n):
         
        # Get the max character from
        # the string
        mx = s[cr]
        for i in range(cr + 1, n):
            mx = max(mx, s[i])
        lst = cr
 
        # Use all the occurrences of the
        # current maximum character
        for i in range(cr,n):
            if (s[i] == mx):
                res += s[i]
                lst = i
 
        # Repeat the steps for the
        # remaining string
        cr = lst + 1
     
    return res
 
# Driver code
if __name__ == '__main__':
    s = "geeksforgeeks"
    n = len(s)
    print(getSubSeq(s, n))
 
# This code is contributed by
# Surendra_Gangwar

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the lexicographically
    // largest sub-sequence of s
    static String getSubSeq(String s, int n)
    {
        String res = "";
        int cr = 0;
        while (cr < n)
        {
 
            // Get the max character from
            // the String
            char mx = s[cr];
            for (int i = cr + 1; i < n; i++)
            {
                mx = (char) Math.Max(mx, s[i]);
            }
            int lst = cr;
 
            // Use all the occurrences of the
            // current maximum character
            for (int i = cr; i < n; i++)
            {
                if (s[i] == mx)
                {
                    res += s[i];
                    lst = i;
                }
            }
 
            // Repeat the steps for
            // the remaining String
            cr = lst + 1;
        }
        return res;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String s = "geeksforgeeks";
        int n = s.Length;
        Console.WriteLine(getSubSeq(s, n));
    }
}
 
// This code is contributed by 29AjayKumar

PHP

<?php
// PHP implementation of the approach
 
// Function to return the lexicographically
// largest sub-sequence of s
function getSubSeq($s, $n)
{
    $res = "";
    $cr = 0;
    while ($cr < $n)
    {
 
        // Get the max character from the string
        $mx = $s[$cr];
        for ($i = $cr + 1; $i < $n; $i++)
            $mx = max($mx, $s[$i]);
        $lst = $cr;
 
        // Use all the occurrences of the
        // current maximum character
        for ($i = $cr; $i < $n; $i++)
            if ($s[$i] == $mx)
            {
                $res .= $s[$i];
                $lst = $i;
            }
 
        // Repeat the steps for the
        // remaining string
        $cr = $lst + 1;
    }
    return $res;
}
 
// Driver code
$s = "geeksforgeeks";
$n = strlen($s);
echo getSubSeq($s, $n);
 
// This code is contributed by
// Akanksha Rai
?>

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function to return the lexicographically
// largest sub-sequence of s
function getSubSeq(s, n)
{
    var res = "";
    var cr = 0;
    while (cr < n) {
 
        // Get the max character from the string
        var mx = s[cr].charCodeAt(0);
        for (var i = cr + 1; i < n; i++)
            mx = Math.max(mx, s[i].charCodeAt(0));
        var lst = cr;
 
        // Use all the occurrences of the
        // current maximum character
        for (var i = cr; i < n; i++)
            if (s[i].charCodeAt(0) == mx) {
                res += s[i];
                lst = i;
            }
 
        // Repeat the steps for the remaining string
        cr = lst + 1;
    }
    return res;
}
 
// Driver code
var s = "geeksforgeeks";
var n = s.length;
document.write( getSubSeq(s, n));
 
// This code is contributed by famously.
</script>
Producción: 

ss

 

Complejidad de tiempo: O(N) donde N es la longitud de la string.
 Espacio Auxiliar: O(N)

Publicación traducida automáticamente

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