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

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

检查字符串中是否存在两个相同的子序列

给定一个字符串,任务是检查给定字符串中是否存在两个相等的子序列。如果两个子序列具有相同的字符以相同的字典顺序排列,但字符的位置与原始字符串中的位置不同,则称这两个子序列相等。
例子:

方法:解决这个问题的方法是检查任何字符是否出现多次。由于匹配子序列的最小长度可以是 1,因此如果一个字符在字符串中出现不止一次,则可能有两个相似的子序列。初始化一个长度为 26 的freq[]数组。遍历字符串并增加字符的频率。遍历 freq 数组并检查 0-26 范围内的任何 i 的 freq[i] 是否大于 1,则有可能。
下面是上述方法的实现。

C++
// C++ program to Check if
// similar subsequences exist or not
#include 
using namespace std;
 
// Function to check if similar subsequences
// occur in a string or not
bool check(string s, int l)
{
 
    int freq[26] = { 0 };
    // iterate and count the frequency
    for (int i = 0; i < l; i++) {
        freq[s[i] - 'a']++; // counting frequency of the letters
    }
 
    // check if frequency is more
    // than once of any character
    for (int i = 0; i < 26; i++) {
        if (freq[i] >= 2)
            return true;
    }
    return false;
}
// Driver Code
int main()
{
    string s = "geeksforgeeks";
    int l = s.length();
    if (check(s, l))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}


Java
// Java program to Check
// if similar subsequences
// exist or not
import java.io.*;
import java.util.*;
import java.util.Arrays;
 
class GFG
{
// Function to check if
// similar subsequences
// occur in a string or not
static boolean check(String s,
                     int l)
{
    int freq[] = new int[26];
    Arrays.fill(freq, 0);
     
    // iterate and count
    // the frequency
    for (int i = 0; i < l; i++)
    {
        // counting frequency
        // of the letters
        freq[s.charAt(i) - 'a']++;
    }
 
    // check if frequency is more
    // than once of any character
    for (int i = 0; i < 26; i++)
    {
        if (freq[i] >= 2)
            return true;
    }
    return false;
}
 
// Driver Code
public static void main(String args[])
{
    String s = "geeksforgeeks";
    int l = s.length();
    if (check(s, l))
        System.out.print("YES");
    else
        System.out.print("NO");
}
}


Python3
# Python 3 program to Check if
# similar subsequences exist or not
 
# Function to check if similar subsequences
# occur in a string or not
def check(s, l):
    freq = [0 for i in range(26)]
     
    # iterate and count the frequency
    for i in range(l):
         
        # counting frequency of the letters
        freq[ord(s[i]) - ord('a')] += 1
         
    # check if frequency is more
    # than once of any character
    for i in range(26):
        if (freq[i] >= 2):
            return True
 
    return False
 
# Driver Code
if __name__ == '__main__':
    s = "geeksforgeeks"
    l = len(s)
    if (check(s, l)):
        print("YES")
    else:
        print("NO")
 
# This code is contributed by
# Sahil_Shelangia


C#
// C# Pprogram to Check if similar subsequences
// exist or not
using System;
using System.Collections.Generic;            
 
class GFG
{
     
// Function to check if similar subsequences
// occur in a string or not
static bool check(String s, int l)
{
    int []freq = new int[26];
     
    // iterate and count the frequency
    for (int i = 0; i < l; i++)
    {
        // counting frequency of the letters
        freq[s[i] - 'a']++;
    }
 
    // check if frequency is more
    // than once of any character
    for (int i = 0; i < 26; i++)
    {
        if (freq[i] >= 2)
            return true;
    }
    return false;
}
 
// Driver Code
public static void Main(String []args)
{
    String s = "geeksforgeeks";
    int l = s.Length;
    if (check(s, l))
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
YES

时间复杂度: O(N)
辅助空间: O(1)
注意:如果提到了相似子序列的长度,那么解决问题的方法也会有所不同。本文讨论了检查字符串是否包含两个长度为 2 或更多的重复子序列的方法。