Longest Common Subsequence (LCS) by repeatedly swapping characters of a string with characters of another string
Given two strings A and B of lengths N and M respectively, the task is to find the length of the longest common subsequence that can be two strings if any character from string A can be swapped with any other character from B any number of times.
Examples:
Input: A = “abdeff”, B = “abbet”
Output: 4
Explanation: Swapping A[5] and B[4] modifies A to “abdeft” and B to “abbef”. LCS of the given strings is “abef”. Therefore, length is 4.
Input: A = “abcd”, B = “ab”
Output: 2
Explanation: LCS of the given strings is “ab”. Therefore, length is 2.
Approach: The idea is based on the observation that if any character from string A can be swapped with any other character from string B, then it is also possible to swap characters within string A and also within string B.
Proof: If characters A[i] and A[j] are required to be swapped, then take a temporary element at any index k in string B. Follow the steps below to solve the problem:
- Swap A[i] with B[k].
- Swap B[k] with A[j].
- Swap B[k] with A[i].
In this way, the characters within a string can be swapped. Now, the elements can be arranged in any order. Therefore, the idea is to find the frequencies of all the characters present in both the strings and divide them equally.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void lcsBySwapping(string A, string B)
{
int N = A.size();
int M = B.size();
int freq[26];
memset (freq, 0, sizeof (freq));
for ( int i = 0; i < A.size(); i++) {
freq[A[i] - 'a' ] += 1;
}
for ( int i = 0; i < B.size(); i++) {
freq[B[i] - 'a' ] += 1;
}
int cnt = 0;
for ( int i = 0; i < 26; i++) {
cnt += freq[i] / 2;
}
cout << min(cnt, min(N, M));
}
int main()
{
string A = "abdeff" ;
string B = "abbet" ;
lcsBySwapping(A, B);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void lcsBySwapping(String A, String B)
{
int N = A.length();
int M = B.length();
int freq[] = new int [ 26 ];
for ( int i = 0 ; i < N; i++)
{
freq[A.charAt(i) - 'a' ] += 1 ;
}
for ( int i = 0 ; i < M; i++)
{
freq[B.charAt(i) - 'a' ] += 1 ;
}
int cnt = 0 ;
for ( int i = 0 ; i < 26 ; i++)
{
cnt += freq[i] / 2 ;
}
System.out.println(Math.min(cnt, Math.min(N, M)));
}
public static void main(String[] args)
{
String A = "abdeff" ;
String B = "abbet" ;
lcsBySwapping(A, B);
}
}
|
Python3
def lcsBySwapping(A, B):
N = len (A)
M = len (B)
freq = [ 0 ] * 26
for i in range ( len (A)):
freq[ ord (A[i]) - ord ( 'a' )] + = 1
for i in range ( len (B)):
freq[ ord (B[i]) - ord ( 'a' )] + = 1
cnt = 0
for i in range ( 26 ):
cnt + = freq[i] / / 2
print ( min (cnt, min (N, M)))
if __name__ = = '__main__' :
A = "abdeff"
B = "abbet"
lcsBySwapping(A, B)
|
C#
using System;
class GFG{
static void lcsBySwapping( string A, string B)
{
int N = A.Length;
int M = B.Length;
int [] freq = new int [26];
for ( int i = 0; i < N; i++)
{
freq[A[i] - 'a' ] += 1;
}
for ( int i = 0; i < M; i++)
{
freq[B[i] - 'a' ] += 1;
}
int cnt = 0;
for ( int i = 0; i < 26; i++)
{
cnt += freq[i] / 2;
}
Console.WriteLine(Math.Min(cnt, Math.Min(N, M)));
}
public static void Main( string [] args)
{
string A = "abdeff" ;
string B = "abbet" ;
lcsBySwapping(A, B);
}
}
|
Javascript
<script>
function lcsBySwapping(A, B)
{
var N = A.length;
var M = B.length;
var freq = new Array(26);
freq.fill(0);
for ( var i = 0; i < A.length; i++) {
freq[A[i].charCodeAt(0) - 'a' .charCodeAt(0)] += 1;
}
for ( var i = 0; i < B.length; i++) {
freq[B[i].charCodeAt(0) - 'a' .charCodeAt(0)] += 1;
}
var cnt = 0;
for ( var i = 0; i < 26; i++) {
cnt += parseInt(freq[i] / 2);
}
document.write( Math.min(cnt, Math.min(N, M)));
}
var A = "abdeff" ;
var B = "abbet" ;
lcsBySwapping(A, B);
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Last Updated :
27 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...