Compruebe si existe alguna subsecuencia en una string que no sea palíndromo

Dada una string de alfabetos ingleses en minúsculas. La tarea es verificar si existe alguna subsecuencia en la string que no sea un palíndromo. Si hay al menos 1 subsecuencia que no es un palíndromo, escriba SÍ, de lo contrario, escriba NO.
Ejemplos
 

Input : str = "abaab"
Output : YES
Subsequences "ab" or "abaa" or "aab", are not palindrome.

Input : str = "zzzz"
Output : NO
All possible subsequences are palindrome.

La observación principal es que si la string contiene al menos dos caracteres distintos, siempre habrá una subsecuencia de al menos dos que no sea un palíndromo. Solo si todos los caracteres de la string son iguales entonces no habrá ninguna subsecuencia que no sea un palíndromo. Porque de manera óptima podemos elegir dos caracteres distintos de una string y colocarlos en el mismo orden uno después de cada uno para formar una string no palindrómica.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
bool isAnyNotPalindrome(string s)
{
    // use set to count number of
    // distinct characters
    set<char> unique;
 
    // insert each character in set
    for (int i = 0; i < s.length(); i++)
        unique.insert(s[i]);
 
    // If there is more than 1 unique
    // characters, return true
    if (unique.size() > 1)
        return true;
    // Else, return false
    else
        return false;
}
 
// Driver code
int main()
{
    string s = "aaaaab";
 
    if (isAnyNotPalindrome(s))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}

Java

// Java program to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
 
import java.util.*;
class GFG
{
     
    // Function to check if there exists
    // at least 1 sub-sequence in a string
    // which is not palindrome
    static boolean isAnyNotPalindrome(String s)
    {
        // use set to count number of
        // distinct characters
        Set<Character> unique=new HashSet<Character>();
     
        // insert each character in set
        for (int i = 0; i < s.length(); i++)
            unique.add(s.charAt(i));
     
        // If there is more than 1 unique
        // characters, return true
        if (unique.size() > 1)
            return true;
        // Else, return false
        else
            return false;
    }
     
    // Driver code
    public static void main(String []args)
    {
        String s = "aaaaab";
     
        if (isAnyNotPalindrome(s))
            System.out.println("YES");
        else
            System.out.println("NO");
     
    }
}

Python3

# Python3 program to check if there exists
# at least 1 sub-sequence in a string
# which is not palindrome
 
 
# Function to check if there exists
# at least 1 sub-sequence in a string
# which is not palindrome
def isAnyNotPalindrome(s):
 
    # use set to count number of
    # distinct characters
    unique=set()
 
    # insert each character in set
    for i in range(0,len(s)):
        unique.add(s[i])
 
    # If there is more than 1 unique
    # characters, return true
    if (len(unique) > 1):
        return True
         
    # Else, return false
    else:
        return False
 
 
# Driver code
if __name__=='__main__':
    s = "aaaaab"
 
    if (isAnyNotPalindrome(s)):
        print("YES")
    else:
        print("NO")
 
# This code is contributed by
# ihritik

C#

// C# program to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
using System;
using System.Collections.Generic;
 
class GFG
{
     
    // Function to check if there exists
    // at least 1 sub-sequence in a string
    // which is not palindrome
    static bool isAnyNotPalindrome(String s)
    {
        // use set to count number of
        // distinct characters
        HashSet<char> unique=new HashSet<char>();
     
        // insert each character in set
        for (int i = 0; i < s.Length; i++)
            unique.Add(s[i]);
     
        // If there is more than 1 unique
        // characters, return true
        if (unique.Count > 1)
            return true;
        // Else, return false
        else
            return false;
    }
     
    // Driver code
    public static void Main(String []args)
    {
        String s = "aaaaab";
     
        if (isAnyNotPalindrome(s))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
 
// This code contributed by Rajput-Ji

Javascript

<script>
 
// JavaScript program to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
 
// Function to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
function isAnyNotPalindrome(s)
{
    // use set to count number of
    // distinct characters
    var unique = new Set();
 
    // insert each character in set
    for (var i = 0; i < s.length; i++)
        unique.add(s[i]);
 
    // If there is more than 1 unique
    // characters, return true
    if (unique.size > 1)
        return true;
    // Else, return false
    else
        return false;
}
 
// Driver code
var s = "aaaaab";
if (isAnyNotPalindrome(s))
    document.write( "YES");
else
    document.write( "NO");
 
 
</script>
Producción: 

YES

 

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

Publicación traducida automáticamente

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