检查日期和月份连接的数字是否可用于形成给定的年份
给定三个字符串D 、 M和Y代表一个日期。任务是检查日期和月份的连接是否产生与年份相同的数字。
例子:
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
方法:给定的问题可以通过使用散列来解决。请按照以下步骤解决给定的问题。
- 声明两个无序映射,例如s1和s2 ,它们将存储(char, boolean)对。
- 创建将存储D + M的字符串total_concat 。
- 遍历total_concat和Y并将字符分别存储在s1和s2中。
- 运行一个 for 循环并在 set1 中插入所有数量的total_concat ,在 set2 中插入 Y。
- 比较两张地图s1和s2 :
- 如果s1 = s2 ,则返回Yes 。
- 否则返回No 。
下面是上述方法的实现:
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 s1;
unordered_map 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 s1
= new Dictionary();
Dictionary s2
= new Dictionary();
// 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
输出
Yes
时间复杂度: O(Y),其中 Y 是年份的大小。
辅助空间: O(1)