📌  相关文章
📜  最长公共子序列(LCS)通过反复交换字符串的字符用另一个字符串的字符

📅  最后修改于: 2021-09-04 09:39:37             🧑  作者: Mango

给定两个长度分别为NM 的字符串AB ,如果字符串A 中的任何字符可以与B 中的任何其他字符交换任意次数,则任务是找到可以是两个字符串的最长公共子序列的长度。

例子:

方法:这个想法基于以下观察:如果字符串A 中的任何字符可以与字符串B 中的任何其他字符交换,那么也可以在字符串A和字符串B 中交换字符。

证明:如果需要交换字符A[i]A[j] ,则在字符串B 中的任何索引k处取一个临时元素。请按照以下步骤解决问题:

  • 将 A[i]B[k]交换。
  • B[k]A[j]交换。
  • B[k]A[i]交换。

这样就可以交换字符串的字符。现在,元素可以按任何顺序排列。因此,这个想法是找到两个字符串存在的所有字符的频率并将它们平均划分。
请按照以下步骤解决问题:

  • 初始化一个数组,比如freq ,大小为26 ,以存储字符串中每个字符的频率。
  • 遍历字符串AB并更新数组freq[]中每个字符的频率
  • 初始化一个变量,比如cnt ,以存储所需的长度。
  • 遍历数组freq[]并将cnt的值增加freq[i] / 2
  • 存储cntNM的最小值 在一个变量中,比如ans
  • 打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the length of LCS
// possible by swapping any character
// of a string with that of another string
void lcsBySwapping(string A, string B)
{
    // Store the size of the strings
    int N = A.size();
    int M = B.size();
 
    // Stores frequency of characters
    int freq[26];
 
    memset(freq, 0, sizeof(freq));
 
    // Iterate over characters of the string A
    for (int i = 0; i < A.size(); i++) {
 
        // Update frequency of character A[i]
        freq[A[i] - 'a'] += 1;
    }
 
    // Iterate over characters of the string B
    for (int i = 0; i < B.size(); i++) {
 
        // Update frequency of character B[i]
        freq[B[i] - 'a'] += 1;
    }
 
    // Store the count of all pairs
    // of similar characters
    int cnt = 0;
 
    // Traverse the array freq[]
    for (int i = 0; i < 26; i++) {
 
        // Update cnt
        cnt += freq[i] / 2;
    }
 
    // Print the minimum of cnt, N and M
    cout << min(cnt, min(N, M));
}
 
// Driver Code
int main()
{
    // Given strings
    string A = "abdeff";
    string B = "abbet";
 
    lcsBySwapping(A, B);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the length of LCS
// possible by swapping any character
// of a string with that of another string
static void lcsBySwapping(String A, String B)
{
     
    // Store the size of the strings
    int N = A.length();
    int M = B.length();
 
    // Stores frequency of characters
    int freq[] = new int[26];
 
    // Iterate over characters of the string A
    for(int i = 0; i < N; i++)
    {
         
        // Update frequency of character A[i]
        freq[A.charAt(i) - 'a'] += 1;
    }
 
    // Iterate over characters of the string B
    for(int i = 0; i < M; i++)
    {
         
        // Update frequency of character B[i]
        freq[B.charAt(i) - 'a'] += 1;
    }
 
    // Store the count of all pairs
    // of similar characters
    int cnt = 0;
 
    // Traverse the array freq[]
    for(int i = 0; i < 26; i++)
    {
         
        // Update cnt
        cnt += freq[i] / 2;
    }
 
    // Print the minimum of cnt, N and M
    System.out.println(Math.min(cnt, Math.min(N, M)));
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given strings
    String A = "abdeff";
    String B = "abbet";
 
    lcsBySwapping(A, B);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to find the length of LCS
# possible by swapping any character
# of a with that of another string
def lcsBySwapping(A, B):
     
    # Store the size of the strings
    N = len(A)
    M = len(B)
 
    # Stores frequency of characters
    freq = [0] * 26
 
    # Iterate over characters of the A
    for i in range(len(A)):
         
        # Update frequency of character A[i]
        freq[ord(A[i]) - ord('a')] += 1
 
    # Iterate over characters of the B
    for i in range(len(B)):
         
        # Update frequency of character B[i]
        freq[ord(B[i]) - ord('a')] += 1
 
    # Store the count of all pairs
    # of similar characters
    cnt = 0
 
    # Traverse the array freq[]
    for i in range(26):
         
        # Update cnt
        cnt += freq[i] // 2
 
    # Print the minimum of cnt, N and M
    print (min(cnt, min(N, M)))
 
# Driver Code
if __name__ == '__main__':
     
    # Given strings
    A = "abdeff"
    B = "abbet"
 
    lcsBySwapping(A, B)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the length of LCS
// possible by swapping any character
// of a string with that of another string
static void lcsBySwapping(string A, string B)
{
 
    // Store the size of the strings
    int N = A.Length;
    int M = B.Length;
 
    // Stores frequency of characters
    int[] freq = new int[26];
 
    // Iterate over characters of the string A
    for(int i = 0; i < N; i++)
    {
         
        // Update frequency of character A[i]
        freq[A[i] - 'a'] += 1;
    }
 
    // Iterate over characters of the string B
    for(int i = 0; i < M; i++)
    {
         
        // Update frequency of character B[i]
        freq[B[i] - 'a'] += 1;
    }
 
    // Store the count of all pairs
    // of similar characters
    int cnt = 0;
 
    // Traverse the array freq[]
    for(int i = 0; i < 26; i++)
    {
         
        // Update cnt
        cnt += freq[i] / 2;
    }
 
    // Print the minimum of cnt, N and M
    Console.WriteLine(Math.Min(cnt, Math.Min(N, M)));
}
 
// Driver Code
public static void Main(string[] args)
{
 
    // Given strings
    string A = "abdeff";
    string B = "abbet";
 
    lcsBySwapping(A, B);
}
}
 
// This code is contributed by ukasp


Javascript


输出:
4

时间复杂度: O(N + M)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live