CPRMT - Common Permutation

no tags 

Given two strings of lowercase letters, a and b, print the longest string x of lowercase letters such that there is a permutation of x that is a subsequence of a and there is a permutation of x that is a subsequence of b.


Input file contains several lines of input. Consecutive two lines make a set of input. That means in the input file line 1 and 2 is a set of input, line 3 and 4 is a set of input and so on. The first line of a pair contains a and the second contains b. Each string is on a separate line and consists of at most 1000 lowercase letters.


For each set of input, output a line containing x. If several x satisfy the criteria above, choose the first one in alphabetical order.


Sample input:
Sample output:

hide comments
masterchef2209: 2018-10-18 15:52:25


shashankpathak: 2018-10-11 09:08:23

how can there be more than 1 possible x as all common characters will be there in x .

deepak097: 2018-08-18 15:17:44

2 freq array that's all O(n) :)

adityad1998: 2018-06-29 14:33:00

Looks difficult but simple AC in 0.00 with arrays.

karan_yadav: 2018-06-25 20:51:47

No need for DP/Binary Search. Though it can be solved using those. It's really up to you.

******************************SPOILER ALERT******************************
AC using 3 arrays (each of length 26).
2 for counts of each alphabet in string a and b.
and the last one for the solution.

sktibrewal: 2018-03-27 23:34:38

Did it using Binary search!

saurav52: 2017-08-12 00:25:32

did it using DP :)

sandeep_4141: 2017-06-14 18:25:17

too easy and straightforward !!!

aditya9125: 2017-04-17 20:22:36

Its easy ,just think simply.

a2j007: 2017-03-17 19:40:46

Last edit: 2017-03-19 04:51:43

Added by:Andrés Leonardo Rojas Duarte
Time limit:0.133s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:University of Alberta Local Contest 1999