📌  相关文章
📜  检查字符串中是否存在非回文的子序列

📅  最后修改于: 2022-05-13 01:57:06.529000             🧑  作者: Mango

检查字符串中是否存在非回文的子序列

给定字符串小写英文字母。任务是检查字符串中是否存在任何不是回文的子序列。如果至少有 1 个子序列不是回文,则打印 YES,否则打印 NO。
例子

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

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

主要观察结果是,如果字符串包含至少两个不同的字符,那么总会有一个长度至少为 2 的子序列不是回文。只有当字符串的所有字符都相同时,才会有任何不是回文的子序列。因为以一种最佳方式,我们可以从一个字符串中选择任意两个不同的字符,并将它们以相同的顺序依次排列,以形成一个非回文字符串。
以下是上述方法的实现:

C++
// C++ program to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
 
#include 
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 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 unique=new HashSet();
     
        // 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 unique=new HashSet();
     
        // 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


输出:
YES

时间复杂度: O(N * logN),其中 N 是字符串的长度。
辅助空间: O(N)