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++
#include <bits/stdc++.h>
using namespace std;
bool check(string D, string M, string Y)
{
unordered_map< char , bool > s1;
unordered_map< char , bool > 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 << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.util.*;
public class GFG
{
static boolean check(String D, String M, String Y)
{
HashMap<Character, Boolean> s1 = new HashMap<>();
HashMap<Character, Boolean> s2 = new HashMap<>();
String total_concat = D + M;
for ( int i = 0 ; i < 4 ; i++) {
s1.put(total_concat.charAt(i), true );
s2.put(Y.charAt(i), true );
}
if (s1.size() != s2.size())
return false ;
else
return s1.equals(s2);
}
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" );
}
}
|
Python3
def check(D, M, Y):
s1 = {}
s2 = {}
total_concat = D + M
for i in range ( 0 , 4 ):
s1[total_concat[i]] = True
s2[Y[i]] = True
if ( len (s1) ! = len (s2)):
return 0
else :
return s1 = = s2
if __name__ = = "__main__" :
D = "20"
M = "12"
Y = "2001"
if (check(D, M, Y)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG
{
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 >();
string total_concat = D + M;
for ( int i = 0; i < 4; i++) {
s1[total_concat[i]] = true ;
s2[Y[i]] = true ;
}
if (s1.Count != s2.Count)
return false ;
else {
return s1.Count == s2.Count
&& !s1.Except(s2).Any();
}
}
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" );
}
}
|
Javascript
<script>
function check(D, M, Y) {
let s1 = new Map();
let s2 = new Map();
let total_concat = D + M;
for (let i = 0; i < 4; i++) {
s1.set(total_concat.charAt(i), true );
s2.set(Y.charAt(i), true );
}
if (s1.size != s2.size)
return 0;
else
return (s1.keys == s2.keys) ? 1 : 0;
}
let D = "20" ;
let M = "12" ;
let Y = "2001" ;
if (check(D, M, Y))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(Y), where Y is size of year.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...