📌  相关文章
📜  检查两个Integer是否互为字母

📅  最后修改于: 2021-04-21 23:57:56             🧑  作者: Mango

给定两个整数AB ,任务是检查给定的数字是否是彼此的字谜。就像字符串一样,如果仅通过对数字进行改组就可以使该数字等于其他数字,则该数字被称为其他数字的类似物。
例子:

方法:创建两个数组freqA []freqB [] ,其中freqA [i]freqB [i]分别将数字i的频率存储在ab中。现在遍历频率数组,对于任何数字i,如果freqA [i]!= freqB [i],那么这些数字就不是彼此的字谜。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
const int TEN = 10;
 
// Function to update the frequency array
// such that freq[i] stores the
// frequency of digit i in n
void updateFreq(int n, int freq[])
{
 
    // While there are digits
    // left to process
    while (n) {
        int digit = n % TEN;
 
        // Update the frequency of
        // the current digit
        freq[digit]++;
 
        // Remove the last digit
        n /= TEN;
    }
}
 
// Function that returns true if a and b
// are anagarams of each other
bool areAnagrams(int a, int b)
{
 
    // To store the frequencies of
    // the digits in a and b
    int freqA[TEN] = { 0 };
    int freqB[TEN] = { 0 };
 
    // Update the frequency of
    // the digits in a
    updateFreq(a, freqA);
 
    // Update the frequency of
    // the digits in b
    updateFreq(b, freqB);
 
    // Match the frequencies of
    // the common digits
    for (int i = 0; i < TEN; i++) {
 
        // If frequency differs for any digit
        // then the numbers are not
        // anagrams of each other
        if (freqA[i] != freqB[i])
            return false;
    }
 
    return true;
}
 
// Driver code
int main()
{
    int a = 240, b = 204;
 
    if (areAnagrams(a, b))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
    static final int TEN = 10;
     
    // Function to update the frequency array
    // such that freq[i] stores the
    // frequency of digit i in n
    static void updateFreq(int n, int [] freq)
    {
     
        // While there are digits
        // left to process
        while (n > 0)
        {
            int digit = n % TEN;
     
            // Update the frequency of
            // the current digit
            freq[digit]++;
     
            // Remove the last digit
            n /= TEN;
        }
    }
     
    // Function that returns true if a and b
    // are anagarams of each other
    static boolean areAnagrams(int a, int b)
    {
     
        // To store the frequencies of
        // the digits in a and b
        int [] freqA = new int[TEN];
        int [] freqB = new int[TEN];
     
        // Update the frequency of
        // the digits in a
        updateFreq(a, freqA);
     
        // Update the frequency of
        // the digits in b
        updateFreq(b, freqB);
     
        // Match the frequencies of
        // the common digits
        for (int i = 0; i < TEN; i++)
        {
     
            // If frequency differs for any digit
            // then the numbers are not
            // anagrams of each other
            if (freqA[i] != freqB[i])
                return false;
        }
        return true;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int a = 204, b = 240;
     
        if (areAnagrams(a, b))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by ihirtik


Python3
# Python3 implementation of the approach
TEN = 10
 
# Function to update the frequency array
# such that freq[i] stores the
# frequency of digit i in n
def updateFreq(n, freq) :
 
    # While there are digits
    # left to process
    while (n) :
        digit = n % TEN
 
        # Update the frequency of
        # the current digit
        freq[digit] += 1
 
        # Remove the last digit
        n //= TEN
 
# Function that returns true if a and b
# are anagarams of each other
def areAnagrams(a, b):
 
    # To store the frequencies of
    # the digits in a and b
    freqA = [ 0 ] * TEN
    freqB = [ 0 ] * TEN
 
    # Update the frequency of
    # the digits in a
    updateFreq(a, freqA)
 
    # Update the frequency of
    # the digits in b
    updateFreq(b, freqB)
 
    # Match the frequencies of
    # the common digits
    for i in range(TEN):
 
        # If frequency differs for any digit
        # then the numbers are not
        # anagrams of each other
        if (freqA[i] != freqB[i]):
            return False
             
    return True
 
# Driver code
a = 240
b = 204
 
if (areAnagrams(a, b)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by
# divyamohan123


C#
// C# implementation of the approach
using System;
 
class GFG
{
    static int TEN = 10;
     
    // Function to update the frequency array
    // such that freq[i] stores the
    // frequency of digit i in n
    static void updateFreq(int n, int [] freq)
    {
     
        // While there are digits
        // left to process
        while (n > 0)
        {
            int digit = n % TEN;
     
            // Update the frequency of
            // the current digit
            freq[digit]++;
     
            // Remove the last digit
            n /= TEN;
        }
    }
     
    // Function that returns true if a and b
    // are anagarams of each other
    static bool areAnagrams(int a, int b)
    {
     
        // To store the frequencies of
        // the digits in a and b
        int [] freqA = new int[TEN];
        int [] freqB = new int[TEN];;
     
        // Update the frequency of
        // the digits in a
        updateFreq(a, freqA);
     
        // Update the frequency of
        // the digits in b
        updateFreq(b, freqB);
     
        // Match the frequencies of
        // the common digits
        for (int i = 0; i < TEN; i++)
        {
     
            // If frequency differs for any digit
            // then the numbers are not
            // anagrams of each other
            if (freqA[i] != freqB[i])
                return false;
        }
        return true;
    }
     
    // Driver code
    public static void Main ()
    {
        int a = 204, b = 240;
     
        if (areAnagrams(a, b))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by ihirtik


Python3
# Python3 implementation of the approach
# Function that returns true if a and b
# are anagarams of each other
def areAnagrams(a, b):
   
    # Converting numbers to strings
    a = str(a)
    b = str(b)
     
    # Checking if the sorting values
    # of two strings are equal
    if(sorted(a) == sorted(b)):
        return True
    else:
        return False
 
 
# Driver code
a = 240
b = 204
 
if (areAnagrams(a, b)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by vikkycirus


输出:
Yes

方法2:使用sorting():

  • 将两个整数转换为字符串。
  • 对字符串排序,然后检查它们是否相等。
  • 如果它们相等,则打印是。
  • 其他没有

下面是实现:

Python3

# Python3 implementation of the approach
# Function that returns true if a and b
# are anagarams of each other
def areAnagrams(a, b):
   
    # Converting numbers to strings
    a = str(a)
    b = str(b)
     
    # Checking if the sorting values
    # of two strings are equal
    if(sorted(a) == sorted(b)):
        return True
    else:
        return False
 
 
# Driver code
a = 240
b = 204
 
if (areAnagrams(a, b)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by vikkycirus

输出:

Yes