- Mathematical, School Programming

Check if digits from concatenation of date and month can be used to form given year

Given three strings D, M, and Y representing a date. The task is to check whether concatenation of date and month results in the same digits as of the year. Examples:Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.Input: D = 20, M = 12, Y = 2001Output: YesExplanation: Below are the operations performed:D + M = 2012, Set1 = 0, 1, 2                      Y = 2001, Set2 = 0, 1, 2Set1 = Set2, Therefore the answer is Yes. Input: D = 26, M = 07, Y = 2001Output: NoApproach: The given problem can be solved by using Hashing. Follow the steps below to solve the given problem. Declare two unordered maps, say s1 and s2 which will store (char, boolean) pair.Create strings total_concat which will store D + M.Traverse both total_concat and Y and store characters in s1 and s2 respectively.Run a for loop and insert all numbers of total_concat in set1 and Y in set2.Compare the two maps s1 and s2:If s1 = s2, return Yes.Else return No.Below is the implementation of the above approach:C++#include using namespace std;bool check(string D, string M, string Y){    unordered_map s1;    unordered_map s2;        string total_concat = D + M;        for (int i = 0; i < 4; i++) {        s1[total_concat[i]] = true;        s2[Y[i]] = true;    }            if (s1.size() != s2.size())        return 0;                else        return (s1 == s2) ? 1 : 0;}int main(){    string D = "20";    string M = "12";    string Y = "2001";    if (check(D, M, Y))        cout