📌  相关文章
📜  找到两个具有至少一个不同索引的最大长度的相等子序列

📅  最后修改于: 2021-10-27 03:26:16             🧑  作者: Mango

给定一个字符串str,任务是找到最大长度K,使得存在两个子序列AB各自的长度的K,使得A = B以及AB之间共同索引的数量为至多K – 1。
例子:

方法:找到任意一对相同的字母,它们之间的字母数量最少,假设这个最小数字是X ,现在问题的答案是len(str) – (X + 1) 。在X中添加一个以不计算一对中的一个字母。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
const int MAX = 26;
 
// Function to return the required
// length of the subsequences
int maxLength(string str, int len)
{
    // To store the result
    int res = 0;
 
    // To store the last visited
    // position of lowercase letters
    int lastPos[MAX];
 
    // Initialisation of frequency array to -1 to
    // indicate no character has previously occured
    for (int i = 0; i < MAX; i++) {
        lastPos[i] = -1;
    }
 
    // For every character of the string
    for (int i = 0; i < len; i++) {
 
        // Get the index of the current character
        int C = str[i] - 'a';
 
        // If the current character has
        // appeared before in the string
        if (lastPos[C] != -1) {
 
            // Update the result
            res = max(len - (i - lastPos[C] - 1) - 1, res);
        }
 
        // Update the last position
        // of the current character
        lastPos[C] = i;
    }
 
    return res;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    int len = str.length();
 
    cout << maxLength(str, len);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
static int MAX = 26;
 
// Function to return the required
// length of the subsequences
static int maxLength(String str, int len)
{
    // To store the result
    int res = 0;
 
    // To store the last visited
    // position of lowercase letters
    int lastPos[] = new int[MAX];
 
    // Initialisation of frequency array to -1 to
    // indicate no character has previously occured
    for (int i = 0; i < MAX; i++)
    {
        lastPos[i] = -1;
    }
 
    // For every character of the String
    for (int i = 0; i < len; i++)
    {
 
        // Get the index of the current character
        int C = str.charAt(i) - 'a';
 
        // If the current character has
        // appeared before in the String
        if (lastPos[C] != -1)
        {
 
            // Update the result
            res = Math.max(len - (i -
                            lastPos[C] - 1) - 1, res);
        }
 
        // Update the last position
        // of the current character
        lastPos[C] = i;
    }
    return res;
}
 
// Driver code
public static void main(String args[])
{
    String str = "geeksforgeeks";
    int len = str.length();
 
    System.out.println(maxLength(str, len));
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Python implementation of the approach
MAX = 26;
 
# Function to return the required
# length of the subsequences
def maxLength(str, len):
 
    # To store the result
    res = 0;
 
    # To store the last visited
    # position of lowercase letters
    lastPos = [0] * MAX;
 
    # Initialisation of frequency array to -1 to
    # indicate no character has previously occured
    for i in range(MAX):
        lastPos[i] = -1;
     
    # For every character of the String
    for i in range(len):
 
        # Get the index of the current character
        C = ord(str[i]) - ord('a');
 
        # If the current character has
        # appeared before in the String
        if (lastPos[C] != -1):
 
            # Update the result
            res = max(len - (i - lastPos[C] - 1) - 1, res);
         
        # Update the last position
        # of the current character
        lastPos[C] = i;
     
    return res;
 
# Driver code
if __name__ == '__main__':
    str = "geeksforgeeks";
    len = len(str);
 
    print(maxLength(str, len));
 
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    static int MAX = 26;
     
    // Function to return the required
    // length of the subsequences
    static int maxLength(string str, int len)
    {
        // To store the result
        int res = 0;
     
        // To store the last visited
        // position of lowercase letters
        int []lastPos = new int[MAX];
     
        // Initialisation of frequency array to -1 to
        // indicate no character has previously occured
        for (int i = 0; i < MAX; i++)
        {
            lastPos[i] = -1;
        }
     
        // For every character of the String
        for (int i = 0; i < len; i++)
        {
     
            // Get the index of the current character
            int C = str[i] - 'a';
     
            // If the current character has
            // appeared before in the String
            if (lastPos[C] != -1)
            {
     
                // Update the result
                res = Math.Max(len - (i -
                                lastPos[C] - 1) - 1, res);
            }
     
            // Update the last position
            // of the current character
            lastPos[C] = i;
        }
        return res;
    }
     
    // Driver code
    public static void Main()
    {
        string str = "geeksforgeeks";
        int len = str.Length;
     
        Console.WriteLine(maxLength(str, len));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
12

时间复杂度: O(n),其中 n 是输入字符串的长度。