Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Last Updated : 07 Jan, 2022
Improve Article
Save Article

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 <bits/stdc++.h>
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<Character, Boolean> s1 = new HashMap<>();
        HashMap<Character, Boolean> 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




<script>
        // JavaScript Program to implement
        // the above approach
 
        // Function to return whether the
        // date is special or not
        function check(D, M, Y) {
 
            let s1 = new Map();
            let s2 = new Map();
 
            // Concatenate date and month
            let total_concat = D + M;
 
            // Insert the elements in set
            for (let i = 0; i < 4; i++) {
                s1.set(total_concat.charAt(i), true);
                s2.set(Y.charAt(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.keys == s2.keys) ? 1 : 0;
        }
 
        // Driver Code
        let D = "20";
        let M = "12";
        let Y = "2001";
 
        if (check(D, M, Y))
            document.write("Yes");
        else
            document.write("No");
 
    // This code is contributed by Potta Lokesh
    </script>

Output

Yes

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

Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!