Knowee
Questions
Features
Study Tools

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. Input Format The first line contains numbers of test cases T The second line has the string P The third line has the string S Output Format Print a single string in a new line for every test case giving the result Constraints 1 <= T <= 1000 Length (P) = 26 1 <= length (S) <= 100 All characters in the string S, P are in lowercase Refer the sample output for formatting Sample Input 2 poliktgbvfredcxwqaz abcd qwryupcsfoghjkldezxvbintma ativedoc Sample Output bdca codevita Explanation The transformed smallest lexicographical strings are in order they would be if order of letters are changed to string P. give java code

Question

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.

Input Format

The first line contains numbers of test cases T

The second line has the string P

The third line has the string S

Output Format

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

Constraints

1 <= T <= 1000

Length (P) = 26

1 <= length (S) <= 100

All characters in the string S, P are in lowercase

Refer the sample output for formatting

Sample Input

2

poliktgbvfredcxwqaz

abcd

qwryupcsfoghjkldezxvbintma

ativedoc

Sample Output

bdca

codevita

Explanation

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem can be solved using a frequency map and a custom comparator for sorting. Here is a Java solution:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T-- > 0) {
            String P = sc.next();
            String S = sc.next();
            int[] freq = new int[26];
            for (char c : S.toCharArray()) {
                freq[c - 'a']++;
            }
            StringBuilder result = new StringBuilder();
            for (char c : P.toCharArray()) {
                while (freq[c - 'a']-- > 0) {
                    result.append(c);
                }
            }
            System.out.println(result);
        }
    }
}

This code works by first reading the number of test cases. For each test case, it reads the new dictionary order and the string to be sorted. It then creates a frequency map of the characters in the string to be sorted. It iterates over the characters in the new dictionary order, appending each character to the result string the number of times it appears in the string to be sorted. Finally, it prints the result string.

This problem has been solved

Similar Questions

Given 2 strings A and B, find the smallest substring of B having all the characters of A, in any order.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line containing 2 space-separated strings - A and B.Output FormatFor each test case, print the length of the smallest substring of B having all the characters of A, separated by newline. If no such substring is found, print -1.Constraints20 points1 <= T <= 1001 <= size(A), size(B) <= 10060 points1 <= T <= 1001 <= size(A), size(B) <= 1000120 points1 <= T <= 1001 <= size(A), size(B) <= 10000General Constraints'a' <= A[i], B[i] <= 'z'ExampleInput4fkqyu frqkzkruqmfqyuzlkygonmwvytbytn uqhmfjaqtgngcwkuzyamnerphfmwbloets lwbcrsfothplxseplrtbshbtstjloxsfdzpd dclzztpjldkndgbdqqzmbpOutput7-1139ExplanationTest Case 1:The smallest substring containing all characters of A is "fqyuzlk", which has a length of 7.Test Case 2:Despite considering all possible substrings of B, we cannot find any substring containing all characters of A.Test Case 3:The smallest substring containing all characters of A is "bcrsfothplxse", which has a length of 13.Test Case 4:The smallest substring containing all characters of A is "ztpjldknd", which has a length of 9.

Given a string without duplicates, print all the permutations of the string in a lexicographically order.Input FormatFirst line of input contains T - number of test cases. Its followed by T lines, each contains a single string consisting only of lowercase Enghish alphabets.ConstraintsFor each test case, print the test case number as shown, followed by all the permutations of the string in a lexicographically order, separated by newline.Output Format1 <= T <= 1001 <= len(str) <= 8Sample Input 02algodsSample Output 0Test Case #1:agloagolalgoalogaoglaolggalogaolglaogloagoalgolalagolaoglgaolgoaloaglogaoagloalgogaloglaolagolgaTest Case #2:dssd

First line of input contains T - number of test cases. Its followed by T lines, each line contains a string of size N, containing only lowercase english alphabets.Output FormatFor each test case, print the length of the largest substring that does not contain any vowel, separated by newline.Constraints1 <= T <= 1001 <= N <= 104ExampleInput3smartinterviewsalgorithms

This exercise is to test your understanding of Java Strings. A sample String declaration:String myString = "Hello World!"The elements of a String are called characters. The number of characters in a String is called the length, and it can be retrieved with the String.length() method.Given two strings of lowercase English letters, and , perform the following operations:Sum the lengths of and .Determine if is lexicographically larger than (i.e.: does come before in the dictionary?).Capitalize the first letter in and and print them on a single line, separated by a space.Input FormatThe first line contains a string . The second line contains another string . The strings are comprised of only lowercase English letters.Output FormatThere are three lines of output:For the first line, sum the lengths of and .For the second line, write Yes if is lexicographically greater than otherwise print No instead.For the third line, capitalize the first letter in both and and print them on a single line, separated by a space.Sample Input 0hellojavaSample Output 09NoHello JavaExplanation 0String is "hello" and is "java". has a length of , and has a length of ; the sum of their lengths is .When sorted alphabetically/lexicographically, "hello" precedes "java"; therefore, is not greater than and the answer is No.When you capitalize the first letter of both and and then print them separated by a space, you get "Hello Java".

Write a Java program to print the smallest and biggest possible palindrome word in a given string. If no palindrome is found in the given string, then, print 'no palindrome'.Input FormatIn the first line read a stringOutput FormatThe first line contains the smallest palindrome word  (or) no palindromeThe nextline contains the biggest palindrome word

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.