Longest common subsequence with permutations allowed
Given two strings in lowercase, find the longest string whose permutations are subsequences of given two strings. The output longest string must be sorted.
Examples:
Input : str1 = "pink", str2 = "kite"
Output : "ik"
The string "ik" is the longest sorted string
whose one permutation "ik" is subsequence of
"pink" and another permutation "ki" is
subsequence of "kite".
Input : str1 = "working", str2 = "women"
Output : "now"
Input : str1 = "geeks" , str2 = "cake"
Output : "ek"
Input : str1 = "aaaa" , str2 = "baba"
Output : "aa"
The idea is to count characters in both strings.
- calculate frequency of characters for each string and store them in their respective count arrays, say count1[] for str1 and count2[] for str2.
- Now we have count arrays for 26 characters. So traverse count1[] and for any index ‘i’ append character (‘a’+i) in resultant string ‘result’ by min(count1[i], count2[i]) times.
- Since we traverse count array in ascending order, our final string characters will be in sorted order.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void longestString(string str1, string str2)
{
int count1[26] = {0}, count2[26]= {0};
for ( int i=0; i<str1.length(); i++)
count1[str1[i]- 'a' ]++;
for ( int i=0; i<str2.length(); i++)
count2[str2[i]- 'a' ]++;
string result;
for ( int i=0; i<26; i++)
for ( int j=1; j<=min(count1[i],count2[i]); j++)
result.push_back( 'a' + i);
cout << result;
}
int main()
{
string str1 = "geeks" , str2 = "cake" ;
longestString(str1, str2);
return 0;
}
|
Java
class GFG {
static void longestString(String str1, String str2) {
int count1[] = new int [ 26 ], count2[] = new int [ 26 ];
for ( int i = 0 ; i < str1.length(); i++) {
count1[str1.charAt(i) - 'a' ]++;
}
for ( int i = 0 ; i < str2.length(); i++) {
count2[str2.charAt(i) - 'a' ]++;
}
String result = "" ;
for ( int i = 0 ; i < 26 ; i++)
{
for ( int j = 1 ; j <= Math.min(count1[i], count2[i]); j++) {
result += ( char )( 'a' + i);
}
}
System.out.println(result);
}
public static void main(String[] args) {
String str1 = "geeks" , str2 = "cake" ;
longestString(str1, str2);
}
}
|
Python3
def longestString(str1, str2):
count1 = [ 0 ] * 26
count2 = [ 0 ] * 26
for i in range ( len (str1)):
count1[ ord (str1[i]) - ord ( 'a' )] + = 1
for i in range ( len (str2)):
count2[ ord (str2[i]) - ord ( 'a' )] + = 1
result = ""
for i in range ( 26 ):
for j in range ( 1 , min (count1[i],
count2[i]) + 1 ):
result = result + chr ( ord ( 'a' ) + i)
print (result)
if __name__ = = "__main__" :
str1 = "geeks"
str2 = "cake"
longestString(str1, str2)
|
C#
using System;
class GFG
{
static void longestString(String str1,
String str2)
{
int []count1 = new int [26];
int []count2 = new int [26];
for ( int i = 0; i < str1.Length; i++)
{
count1[str1[i] - 'a' ]++;
}
for ( int i = 0; i < str2.Length; i++)
{
count2[str2[i] - 'a' ]++;
}
String result = "" ;
for ( int i = 0; i < 26; i++)
{
for ( int j = 1;
j <= Math.Min(count1[i],
count2[i]); j++)
{
result += ( char )( 'a' + i);
}
}
Console.Write(result);
}
public static void Main()
{
String str1 = "geeks" , str2 = "cake" ;
longestString(str1, str2);
}
}
|
PHP
<?php
function longestString( $str1 , $str2 )
{
$count1 = array_fill (0, 26, NULL);
$count2 = array_fill (0, 26, NULL);
for ( $i = 0; $i < strlen ( $str1 ); $i ++)
$count1 [ord( $str1 [ $i ]) - ord( 'a' )]++;
for ( $i = 0; $i < strlen ( $str2 ); $i ++)
$count2 [ord( $str2 [ $i ]) - ord( 'a' )]++;
$result = "" ;
for ( $i = 0; $i < 26; $i ++)
for ( $j = 1; $j <= min( $count1 [ $i ],
$count2 [ $i ]); $j ++)
$result = $result . chr (ord( 'a' ) + $i );
echo $result ;
}
$str1 = "geeks" ;
$str2 = "cake" ;
longestString( $str1 , $str2 );
?>
|
Javascript
<script>
function min(a, b)
{
if (a < b)
return a;
else
return b;
}
function longestString( str1, str2)
{
var count1 = new Array(26);
var count2 = new Array(26);
count1.fill(0);
count2.fill(0);
for ( var i = 0; i < str1.length; i++) {
count1[str1.charCodeAt(i) -97]++;
}
for ( var i = 0; i < str2.length; i++) {
count2[str2.charCodeAt(i) - 97]++;
}
var result = "" ;
for ( var i = 0; i < 26; i++)
{
for ( var j = 1; j <= min(count1[i], count2[i]); j++) {
result += String.fromCharCode(97 + i);
}
}
document.write(result);
}
var str1 = "geeks" ;
var str2 = "cake" ;
longestString(str1, str2);
</script>
|
Time Complexity: O(m + n), where m and n are lengths of input strings.
Auxiliary Space: O(1)
If you have another approach to solve this problem then please share.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Last Updated :
05 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...