# Lexi String - Tcs Codevita Previos Year Question With Solution

Mon, 27 Jul, 2020

## Lexi String

### Problem Description

Little Jill jumbled up the order of the letters in our dictionary. Now, Jack uses this list to find the smallest lexicographical string that can be made out of this new order. Can you help him?

You are given a string P that denotes the new order of letters in the English dictionary.

You need to print the smallest lexicographic string made from the given string S.

### Constraints

1 <= T <= 1000

Length (P) = 26

1 <= length (S) <= 100

All characters in the string S, P are in lowercase

### Input Format

The first line contains number of test cases T

The second line has the string P

The third line has the string S

### Output

Print a single string in a new line for every test case giving the result

```
```

### Explanation

Example 1

Input

2

polikujmnhytgbvfredcxswqaz

abcd

qwryupcsfoghjkldezxvbintma

ativedoc

Output

bdca

codevita

Explanation

The transformed smallest lexicographical strings are in order they would be if order of letters are changed to string P

Solution in java

``````import java.util.Scanner;
import java.util.TreeSet;

public class MyClass {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
StringBuffer sb=new StringBuffer();
while(n-->0){
String p=sc.next()+sc.nextLine();
String s=sc.next()+sc.nextLine();
sb.append(lexi(p,s)+"\n");
}
System.out.print(sb);

}

private static String lexi(String p, String s) {
String ans="";
TreeSet set=new TreeSet<>();

for(int i=0;i<s.length();i++)
{
int index=p.indexOf(s.charAt(i));
if(set.contains(index)){
for(int j=index;j<=(p.lastIndexOf(s.charAt(i)));j++){
int in=p.indexOf(s.charAt(i),j);
if(set.contains(in))
continue;
else
}
}
else
}
for(int i:set)
ans=ans+p.charAt(i);
return ans;
}

}``````

Solution in python

``````test=int(input())
for i in range(test):
alpha=input()
str1=input()
l=[]
for i in str1:
l.append(i)
di={}
c=0
for i in alpha:
di[c]=i
c+=1
s1=''
for j in di.values():
if j in l:
s1=s1+j
print(s1)
test-=1``````