QM18A - Editorial

PROBLEM LINK:

Practice
Contest

Author: Chandan Boruah
Tester: Harsh Raj
Editorialist: Dev Vaghani

DIFFICULTY:

Simple

PREREQUISITES:

Brute force.

PROBLEM:

Given a 26 digit number ‘A’. Each digit representing the english alphabets in ascending order, in smaller case. Also given a second number ‘S’. Each digit in ‘S’ is an alias of each digit in ‘A’. Both numbers might have leading zeroes. ‘S’ won’t contain digits that are not in ‘A’. Print the lexicographically smallest string that can be printed by replacing the digits in ‘S’ with an alias from ‘A’.

QUICK EXPLANATION:

First iterate through the second string and in a nested loop iterate through the first string. Whenever there is a match in the digits we take that digit’s character representation of string ‘A’.

EXPLANATION:

First iterate through the second string and in a nested loop iterate through the first string. Whenever there is a match in the digits we take that digit’s character representation of string ‘A’ and append it to output. Since, a lexicographically smallest string is expected, and since the alphabets represented by the first string are in ascending order, so the first instance of a match is taken.

SOLUTIONS:

Setter's Solution
using System;
using System.Collections.Generic;
class some
{
	public static void Main()
	{	
		string a=Console.ReadLine();
		string b=Console.ReadLine();
		string ret="";
		string cc="abcdefghijklmnopqrstuvwxyz";
		for(int i=0;i<b.Length;i++)
		{	
			ret+=cc[a.IndexOf(b[i])];
		}
		Console.WriteLine(ret);
	}
}
Tester's Solution
   s = input()
   alphabets = 'abcdefghijklmnopqrstuvwxyz'
    maps = {}
     
    for index, ch in enumerate(s):
    	try:
    		maps[ch] = min(maps[ch], alphabets[index])
    	except:
    		maps[ch] = alphabets[index]
     
    ans = ''
    s = input()
    for ch in s:
    	ans += maps[ch]
     
    print(ans)
    # print(maps)
Editorialist's Solution
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,b;
    cin>>a;
    cin>>b;
    long n,m;
    n=a.size();
    m=b.size();
    for(long i=0;i<m;i++)
    {
	for(long j=0;j<n;j++)
	{
	    if(b[i]==a[j])
	    {
	        cout<<(char)(j+97);
	        break;
	    }
	    
	}
    }
}
2 Likes