给定两个正整数A和C ,任务是检查数字B是否存在,使得A + B = C且B是A的字谜。如果发现是真的,则打印“是” 。否则,打印“否” 。
Input: A = 123, C = 354
Output: YES
Explanation:
231 is an anagram of A and 123 + 231 = 354
Therefore, the required output is “YES”.
Input: A = 123, C = 234
Output: NO
原始的方法:要解决这个问题最简单的办法是生成的数字所有可能的排列,并检查是否有之和的数字的当前排列等于C或没有。如果发现是真的,则打印“是” 。否则,如果找不到这样的排列,请打印“ NO” 。
时间复杂度: O(log 10 (N)!)
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
A + B = C
B = C – A
请按照以下步骤解决问题:
- 检查(C – A)是否为A的字谜。如果发现是真的,则打印“是” 。
- 否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to check if an integer B exists
// such that A + B = C and B is an anagram of A
void CheckExistsABCAnagramOfA(int A, int C)
{
int B = C - A;
// Stores A in string format
string a = to_string(A);
// Stores B in string format
string b = to_string(B);
// Sort both the strings
sort(a.begin(), a.end());
sort(b.begin(), b.end());
// Checks if both strings
// are equal
if (a == b) {
cout << "YES\n";
}
else {
cout << "NO\n";
}
}
// Drivers Code
int main()
{
int A = 123, C = 354;
CheckExistsABCAnagramOfA(A, C);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to check if an integer B exists
// such that A + B = C and B is an anagram of A
static void CheckExistsABCAnagramOfA(int A, int C)
{
int B = C - A;
// Stores A in String format
String a = String.valueOf(A);
// Stores B in String format
String b = String.valueOf(B);
// Sort both the Strings
a = sortString(a);
b = sortString(b);
// Checks if both Strings
// are equal
if (a.equals(b))
{
System.out.print("YES\n");
}
else
{
System.out.print("NO\n");
}
}
static String sortString(String inputString)
{
// convert input string to char array
char tempArray[] = inputString.toCharArray();
// sort tempArray
Arrays.sort(tempArray);
// return new sorted string
return new String(tempArray);
}
// Drivers Code
public static void main(String[] args)
{
int A = 123, C = 354;
CheckExistsABCAnagramOfA(A, C);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program to implement the above
# approach
# Function to check if an integer B exists
# such that A + B = C and B is an anagram of A
def CheckExistsABCAnagramOfA(A, C):
B = C - A
# To convert number to list of integers
a = [int(x) for x in str(A)]
# To convert number to list of integers
b = [int(x) for x in str(B)]
# Sort both the strings
a.sort()
b.sort()
# Checks if both strings
# are equal
if (a == b):
print("YES")
else:
print("NO")
# Driver Code
A = 123
C = 354
CheckExistsABCAnagramOfA(A, C)
# This code is contributed by sanjoy_62
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to check if an integer B
// exists such that A + B = C and B
// is an anagram of A
static void CheckExistsABCAnagramOfA(int A, int C)
{
int B = C - A;
// Stores A in String format
String a = String.Join("", A);
// Stores B in String format
String b = String.Join("", B);
// Sort both the Strings
a = sortString(a);
b = sortString(b);
// Checks if both Strings
// are equal
if (a.Equals(b))
{
Console.Write("YES\n");
}
else
{
Console.Write("NO\n");
}
}
static String sortString(String inputString)
{
// Convert input string to char array
char []tempArray = inputString.ToCharArray();
// Sort tempArray
Array.Sort(tempArray);
// Return new sorted string
return new String(tempArray);
}
// Driver Code
public static void Main(String[] args)
{
int A = 123, C = 354;
CheckExistsABCAnagramOfA(A, C);
}
}
// This code is contributed by shikhasingrajput
输出:
YES
时间复杂度: O(log 10 (A)* log(log 10 (A)))
辅助空间: O(log 10 (A))