Subsecuencia más larga de una string que contiene solo consonantes

Dada una string que contiene solo alfabetos ingleses, la tarea es imprimir la subsecuencia más larga que contiene solo consonantes .
Ejemplos: 
 

Entrada: str = “geeksforgeeks” 
Salida: gksfrgks 
La subsecuencia más larga que contiene consonantes será la string de todas las consonantes en la string dada. 
Aquí, viene dado por la string “gksfrgks”.
Entrada: str = “HelloWorld” 
Salida: HllWrld 
 

Acercarse : 
 

  • Primero, recorreremos la string dada.
  • Incluiremos todas las consonantes que encontremos durante nuestro recorrido en la string de respuesta.
  • Una vez que se ha encontrado toda la string inicial, tenemos la subsecuencia más larga que contiene solo consonantes con nosotros.

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

C++

// C++ program to find the longest subsequence
// which contain all consonants
#include <bits/stdc++.h>
 
using namespace std;
 
// Returns true if x is consonants.
bool isConsonants(char x)
{
    // Function to check whether a character is
    // consonants or not
    x = tolower(x);
    return !(x == 'a' || x == 'e' || x == 'i'
                      || x == 'o' || x == 'u');
}
 
// Function to find the longest subsequence
// which contain all consonants
string longestConsonantsSubsequence(string str)
{
    string answer = "";
    int n = str.size();
 
    for (int i = 0; i < n; i++) {
        if (isConsonants(str[i])) {
            answer += str[i];
        }
    }
 
    return answer;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
     
    // Function call
    cout << longestConsonantsSubsequence(str) << endl;
 
    return 0;
}

Java

// Java program to find the longest subsequence
// which contain all consonants
class GFG{
 
// Returns true if x is consonants.
static boolean isConsonants(char x)
{
     
    // Function to check whether a character
    // is consonants or not
    x = Character.toLowerCase(x);
    return !(x == 'a' || x == 'e' ||
             x == 'i' || x == 'o' ||
             x == 'u');
}
 
// Function to find the longest subsequence
// which contain all consonants
static String longestConsonantsSubsequence(String str)
{
    String answer = "";
    int n = str.length();
 
    for (int i = 0; i < n; i++)
    {
        if (isConsonants(str.charAt(i)))
        {
            answer += str.charAt(i);
        }
    }
    return answer;
}
 
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
     
    // Function call
    System.out.print(longestConsonantsSubsequence(str) + "\n");
}
}
 
// This code is contributed by Amit Katiyar

Python3

# Python3 program to find the longest subsequence
# which contain all consonants
 
# Returns true if x is consonants.
def isComsomamts(x):
     
    x = x.lower()
    return not (x == 'a' or x == 'e' or
                x == 'i' or x == 'o' or
                x == 'u')
             
# Function to find the longest subsequence
# which contain all consonants
def longestConsonantsSubsequence(s):
     
    answer = ''
    n = len(s)
     
    for i in range(n):
        if isComsomamts(s[i]):
            answer += s[i]
             
    return answer
 
# Driver code
s = 'geeksforgeeks'
 
# Function call
print(longestConsonantsSubsequence(s))
 
# This code is contributed by rutvik_56

C#

// C# program to find the longest subsequence
// which contain all consonants
using System;
 
class GFG{
     
// Returns true if x is consonants.
static bool isConsonants(char x)
{
     
    // Function to check whether a 
    // character is consonants or not
    x = Char.ToLower(x);
    return !(x == 'a' || x == 'e' ||
             x == 'i' || x == 'o' ||
             x == 'u');
}
     
// Function to find the longest subsequence
// which contain all consonants
static string longestConsonantsSubsequence(string str)
{
    string answer = "";
    int n = str.Length;
     
    for(int i = 0; i < n; i++)
    {
       if (isConsonants(str[i]))
       {
           answer += str[i];
       }
    }
    return answer;
}
 
// Driver code
static void Main()
{
    string str = "geeksforgeeks";
     
    // Function call
    Console.Write(longestConsonantsSubsequence(str) + "\n");
}
}
 
// This code is contributed by divyeshrabadiya07   

Javascript

<script>
      // JavaScript program to find the longest subsequence
      // which contain all consonants
      // Returns true if x is consonants.
      function isConsonants(x) {
        // Function to check whether a
        // character is consonants or not
        x = x.toLowerCase();
        return !(x === "a" || x === "e" || x === "i" || x === "o" || x === "u");
      }
 
      // Function to find the longest subsequence
      // which contain all consonants
      function longestConsonantsSubsequence(str) {
        var answer = "";
        var n = str.length;
 
        for (var i = 0; i < n; i++) {
          if (isConsonants(str[i])) {
            answer += str[i];
          }
        }
        return answer;
      }
 
      // Driver code
      var str = "geeksforgeeks";
 
      // Function call
      document.write(longestConsonantsSubsequence(str) + "<br>");
       
      // This code is contributed by rdtank.
    </script>
Producción: 

gksfrgks

 

Complejidad temporal: O(n)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

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