 Open in App
Not now

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

• Last Updated : 07 Jan, 2022

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:

Input: D = 20, M = 12, Y = 2001
Output: Yes
Explanation: Below are the operations performed:
D + M = 2012, Set1 = 0, 1, 2
Y = 2001, Set2 = 0, 1, 2
Set1 = Set2, Therefore the answer is Yes.

Input: D = 26, M = 07, Y = 2001
Output: No

Approach: 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++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to return whether the` `// date is special or not` `bool` `check(string D, string M, string Y)` `{`   `    ``unordered_map<``char``, ``bool``> s1;` `    ``unordered_map<``char``, ``bool``> s2;`   `    ``// Concatenate date and month` `    ``string total_concat = D + M;`   `    ``// Insert the elements in set` `    ``for` `(``int` `i = 0; i < 4; i++) {` `        ``s1[total_concat[i]] = ``true``;` `        ``s2[Y[i]] = ``true``;` `    ``}`   `    ``// Return 0 if size of both set` `    ``// are unequal` `    ``if` `(s1.size() != s2.size())` `        ``return` `0;`   `    ``// Return 1 if both set contains` `    ``// same set of numbers otherwise` `    ``// return 0` `    ``else` `        ``return` `(s1 == s2) ? 1 : 0;` `}`   `// Driver Code` `int` `main()` `{` `    ``string D = ``"20"``;` `    ``string M = ``"12"``;` `    ``string Y = ``"2001"``;`   `    ``if` `(check(D, M, Y))` `        ``cout << ``"Yes"``;` `    ``else` `        ``cout << ``"No"``;` `}`

## Java

 `// Java implementation for the above approach` `import` `java.util.*;`   `public` `class` `GFG` `{` `    ``// Function to return whether the` `    ``// date is special or not` `    ``static` `boolean` `check(String D, String M, String Y)` `    ``{`   `        ``HashMap s1 = ``new` `HashMap<>();` `        ``HashMap s2 = ``new` `HashMap<>();`   `        ``// Concatenate date and month` `        ``String total_concat = D + M;`   `        ``// Insert the elements in set` `        ``for` `(``int` `i = ``0``; i < ``4``; i++) {` `            ``s1.put(total_concat.charAt(i), ``true``);` `            ``s2.put(Y.charAt(i), ``true``);` `        ``}`   `        ``// Return 0 if size of both set` `        ``// are unequal` `        ``if` `(s1.size() != s2.size())` `            ``return` `false``;`   `        ``// Return 1 if both set contains` `        ``// same set of numbers otherwise` `        ``// return 0` `        ``else` `            ``return` `s1.equals(s2);` `    ``}` `  `  `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args) {` `        `  `        ``String D = ``"20"``;` `        ``String M = ``"12"``;` `        ``String Y = ``"2001"``;` ` `  `        ``if` `(check(D, M, Y))` `            ``System.out.println(``"Yes"``);` `        ``else` `            ``System.out.println(``"No"``);` `    ``}` `}`   `// This code is contributed by Samim Hossain Mondal`

## Python3

 `# python program for the above approach`   `# Function to return whether the` `# date is special or not` `def` `check(D, M, Y):`   `    ``s1 ``=` `{}` `    ``s2 ``=` `{}`   `    ``# Concatenate date and month` `    ``total_concat ``=` `D ``+` `M`   `    ``# Insert the elements in set` `    ``for` `i ``in` `range``(``0``, ``4``):` `        ``s1[total_concat[i]] ``=` `True` `        ``s2[Y[i]] ``=` `True`   `    ``# Return 0 if size of both set` `    ``# are unequal` `    ``if` `(``len``(s1) !``=` `len``(s2)):` `        ``return` `0`   `    ``# Return 1 if both set contains` `    ``# same set of numbers otherwise` `    ``# return 0` `    ``else``:` `      ``return` `s1 ``=``=` `s2`   `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:`   `    ``D ``=` `"20"` `    ``M ``=` `"12"` `    ``Y ``=` `"2001"`   `    ``if` `(check(D, M, Y)):` `        ``print``(``"Yes"``)` `    ``else``:` `        ``print``(``"No"``)`   `    ``# This code is contributed by rakeshsahni`

## C#

 `// C# implementation for the above approach` `using` `System;` `using` `System.Collections.Generic;` `using` `System.Linq;`   `public` `class` `GFG` `{` `  `  `  ``// Function to return whether the` `  ``// date is special or not` `  ``static` `bool` `check(``string` `D, ``string` `M, ``string` `Y)` `  ``{`   `    ``Dictionary<``char``, ``bool``> s1` `      ``= ``new` `Dictionary<``char``, ``bool``>();` `    ``Dictionary<``char``, ``bool``> s2` `      ``= ``new` `Dictionary<``char``, ``bool``>();`   `    ``// Concatenate date and month` `    ``string` `total_concat = D + M;`   `    ``// Insert the elements in set` `    ``for` `(``int` `i = 0; i < 4; i++) {` `      ``s1[total_concat[i]] = ``true``;` `      ``s2[Y[i]] = ``true``;` `    ``}`   `    ``// Return 0 if size of both set` `    ``// are unequal` `    ``if` `(s1.Count != s2.Count)` `      ``return` `false``;`   `    ``// Return 1 if both set contains` `    ``// same set of numbers otherwise` `    ``// return 0` `    ``else` `{`   `      ``return` `s1.Count == s2.Count` `        ``&& !s1.Except(s2).Any();` `    ``}` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main(``string``[] args)` `  ``{`   `    ``string` `D = ``"20"``;` `    ``string` `M = ``"12"``;` `    ``string` `Y = ``"2001"``;`   `    ``if` `(check(D, M, Y))` `      ``Console.WriteLine(``"Yes"``);` `    ``else` `      ``Console.WriteLine(``"No"``);` `  ``}` `}`   `// This code is contributed by ukasp.`

## Javascript

 ``

Output

`Yes`

Time Complexity: O(Y), where Y is size of year.

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles