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

📅  最后修改于: 2021-06-25 20:55:36             🧑  作者: 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


输出:
12

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。