Lucky Mask

Practice this coding question on Coding Gurukul.

Problem statement

A ticket printer assigns increasing positive serial numbers. A display filter hides every digit except 4 and 7, preserving the remaining digits in their original order. An operator has just printed serial a and wants the next possible serial whose filtered display is exactly the requested lucky code b. Your task Find the smallest integer c strictly greater than a such that removing all digits other than 4 and 7 from c leaves exactly b. Important details The requested code contains only 4 and 7 and is nonempty. Extra lucky digits cannot be ignored. Ordinary digits may occur before, between or after the lucky digits. Serial a itself is excluded even if it already has the right mask. Worked example After a = 40, requesting b = 47 gives c = 47. After a = 47 with the same request, the next answer is 147: it filters to 47 and is strictly larger. The serial 447 does not qualify for code 47 because its filtered display is 447.

Input format

Read a b.

Output format

Print the smallest qualifying serial c.

Constraints

1 <= a, b <= 100000; every digit of b is 4 or 7.

Sample input

40 47

Sample output

47

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…