Longest Common Subsequence (LCS) by Repeatedly Swapping Characters of a String with Characters of Another String


Longest Common Subsequence (LCS) is a classic problem in computer science that involves finding the longest subsequence that is present in two given strings. In this tutorial, we will explore a unique approach to solving this problem, which involves repeatedly swapping characters between the two strings until we find the LCS. This approach involves a bit of creativity and is not commonly used, but it can be useful in certain situations. We will be using the C++ programming language to implement this solution, and we will provide a step-by-step guide on how to do so.

So, let's dive in and see how we can find the LCS by swapping characters!

Problem Statement

The objective of the problem is to determine the length of the longest common subsequence that can be formed from two given strings, A and B. In this scenario, any character from string A can be swapped with any other character from string B any number of times. The lengths of strings A and B are N and M respectively.

Sample Examples

Example 1

Given two strings A and B, where A = “abdeff” and B = “abbet”, the output is 4. This means that by swapping any character in A with any other character in B any number of times, the longest common subsequence between the two strings is of length 4. For example, by swapping A[5] and B[4], A becomes “abdeft” and B becomes “abbef”. The resulting modified strings have the longest common subsequence of "abef", with a length of 4.

Example 2

Given two strings A and B, where A = “abcd” and B = “ab”, the output is 2. This means that by swapping any character in A with any other character in B any number of times, the longest common subsequence between the two strings is of length 2. The resulting modified strings have the longest common subsequence of "ab", with a length of 2.

Methodology

The approach is founded on the observation that if swapping characters of string A with characters of string B is allowed, then swapping characters within string A and within string B is also permissible.

Justification

If we need to swap characters A[i] and A[j], we can choose an arbitrary index k in string B, and then follow these steps to solve the problem:

  • Swap A[i] with B[k].

  • Swap B[k] with A[j].

  • Swap B[k] with A[i].

So, by swapping characters in this manner, it is possible to rearrange the elements within a string. This allows for finding the frequencies of all characters present in both strings and dividing them equally.

Algorithm

The following steps can be followed to solve the problem:

STEP 1: Create an array of size 26, called freq, to store the frequency of each character present in the strings.

STEP 2: Traverse through the strings A and B and update the frequency of each character in the array freq[].

STEP 3: Create a variable cnt to store the required length.

STEP 4: Traverse through the array freq[] and increase the value of cnt by freq[i] / 2.

STEP 5: Store the minimum of cnt, N, and M in a variable ans.

STEP 6: Print the value of ans as the output.

Now, let’s understand the implementation of this problem statement with an example using C++.

Example

Implementation of the above algorithm using C++

The below program finds the length of the longest common subsequence of two strings by allowing any character from one string to be swapped with any character from the other string any number of times. The program uses an array 'freq' of size 26 to count the frequency of each character in both strings. It then counts the number of pairs of similar characters in the 'freq' array and returns the minimum of that count and the lengths of the two strings.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int lcsBySwapping(string s1, string s2) {
    int n = s1.size(), m = s2.size(), cnt = 0;
    int freq[26] = { 0 };
    // Count the frequency of each character in both strings
    for (int i = 0; i < n; i++)
        freq[s1[i] - 'a']++;
    for (int i = 0; i < m; i++)
        freq[s2[i] - 'a']++;
    // Count the number of pairs of similar characters
    for (int i = 0; i < 26; i++)
        cnt += freq[i] / 2;
    // Return the minimum of cnt, n and m
    return min(cnt, min(n, m));
}
int main() {
    string s1 = "abcd";
    string s2 = "ab";
    cout << "Input:\n";
    cout << "String 1: " << s1 << endl;
    cout << "String 2: " << s2 << endl;
    int lcsLength = lcsBySwapping(s1, s2);
    cout << "Output:\n";
    cout << "LCS: " << lcsLength << endl;
    return 0;
}

Output

Input:
String 1: abcd
String 2: ab
Output:
LCS: 2

Conclusion

To sum up, in this tutorial, we have discussed an approach to finding the length of the longest common subsequence between two given strings by allowing the swapping of characters from one string with characters from the other string. The proposed algorithm has a time complexity of O(N + M) and auxiliary space complexity of O(1). This approach can be useful in solving certain string-related problems.

Updated on: 08-Sep-2023

51 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements