给定两个整数A和C,任务是检查是否存在数量的排列,使得数量之和其排列是等于C。
例子:
Input: A = 133, C = 446
Output: Yes
Explanation: One of the permutation of A is 313. Therefore, sum = 133 + 313 = 446, which is equal to C.
Input: A = 200, C = 201
Output: No
朴素方法:解决问题的最简单方法是生成数字A 的所有排列并将其与A的原始值相加。现在,检查它们的总和是否等于C。
时间复杂度: O((log 10 A)!)
辅助空间: O(1)
高效方法:为了优化上述方法,这个想法是减去A的从C值,并检查是否存在A的置换,它等于C和A或不之间的差异。请按照以下步骤解决问题:
- 从C 中减去A并检查C是否小于0 。如果发现是真的,则打印“否”。
- 否则,请执行以下步骤:
- 将A转换为其等效的字符串表示形式并将其存储在一个变量中,例如S 。
- 将C转换为其等效的字符串表示形式并将其存储在一个变量中,例如K 。
- 对新生成的字符串S和K 进行排序。
- 如果S等于K ,则打印“是”以及排列。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if there
// exists a permutation of a number
// A whose sum with A results to C
void checkPermutation(int a, int c)
{
// Subtract a from c
c -= a;
// Check if c is less than 0
if (c <= 0) {
// If true, print "No"
cout << "No";
return;
}
// Otherwise, convert a to its
// equivalent string
string s = to_string(a);
// convert c to its
// equivalent string
string k = to_string(c);
// Sort string s
sort(s.begin(), s.end());
// Sort string k
sort(k.begin(), k.end());
// If both strings are equal,
// then print "Yes"
if (k == s) {
cout << "Yes\n"
<< c;
}
// Otherwise, print "No"
else {
cout << "No";
}
}
// Driver Code
int main()
{
int A = 133, C = 446;
checkPermutation(A, C);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if there
// exists a permutation of a number
// A whose sum with A results to C
static void checkPermutation(int a, int c)
{
// Subtract a from c
c -= a;
// Check if c is less than 0
if (c <= 0)
{
// If true, print "No"
System.out.println("No");
return;
}
// Otherwise, convert a to its
// equivalent string
String s = sortString(Integer.toString(a));
// Convert c to its
// equivalent string
String k = sortString(Integer.toString(c));
// If both strings are equal,
// then print "Yes"
if (k.equals(s))
{
System.out.println("Yes");
System.out.println(c);
}
// Otherwise, print "No"
else
{
System.out.println("No");
}
}
// Method to sort a string alphabetically
public 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);
}
// Driver code
public static void main(String[] args)
{
int A = 133, C = 446;
checkPermutation(A, C);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to check if there
# exists a permutation of a number
# A whose sum with A results to C
def checkPermutation(a, c):
# Subtract a from c
c -= a
# Check if c is less than 0
if (c <= 0):
# If true, print "No"
print("No")
return
# Otherwise, convert a to its
# equivalent string
s = str(a)
# convert c to its
# equivalent string
k = str(c)
res = ''.join(sorted(s))
# Sort string s
s = str(res)
# Sort string k
res = ''.join(sorted(k))
k = str(res)
# If both strings are equal,
# then print "Yes"
if (k == s):
print("Yes")
print(c)
# Otherwise, print "No"
else:
print("No")
# Driver Code
if __name__ == '__main__':
A = 133
C = 446
checkPermutation(A, C)
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check if there
// exists a permutation of a number
// A whose sum with A results to C
static void checkPermutation(int a, int c)
{
// Subtract a from c
c -= a;
// Check if c is less than 0
if (c <= 0) {
// If true, print "No"
Console.Write("No");
return;
}
// Otherwise, convert a to its
// equivalent string
string s = a.ToString();
// convert c to its
// equivalent string
string k = c.ToString();
// Sort string s
char[] arr = s.ToCharArray();
Array.Sort(arr);
// Sort string k
char[] brr = k.ToCharArray();
Array.Sort(brr);
// If both strings are equal,
// then print "Yes"
if (String.Join("", brr) == String.Join("", arr)) {
Console.Write("Yes\n" + c);
}
// Otherwise, print "No"
else {
Console.WriteLine("No");
}
}
// Driver Code
public static void Main()
{
int A = 133, C = 446;
checkPermutation(A, C);
}
}
// This code is contributed by ukasp.
Javascript
输出:
Yes
313
时间复杂度: O((log 10 A)*log (log 10 A))
辅助空间: O(log 10 A)