Python Program to print all distinct uncommon digits present in two given numbers
Given two positive integers A and B, the task is to print the distinct digits in descending order, which are not common in the two numbers.
Examples:
Input: A = 378212, B = 78124590
Output: 9 5 4 3 0
Explanation: All distinct digits present in the two numbers are {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. The digits {1, 2, 6, 7} are common in both numbers.
Input: A = 100, B = 273
Output: 7 3 2 1 0
Explanation: All distinct digits present in the two numbers are {0, 1, 2, 3, 7}. The digits {0, 1, 2, 3, 7} are common in both numbers.
Approach: The problem can be solved using sets, and lists in python. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
Python3
def disticntUncommonDigits(A, B):
A = str (A)
B = str (B)
lis1 = list ( map ( int , A))
lis2 = list ( map ( int , B))
lis1 = set (lis1)
lis2 = set (lis2)
lis = lis1.symmetric_difference(lis2)
lis = list (lis)
lis.sort(reverse = True )
for i in lis:
print (i, end = " " )
A = 378212
B = 78124590
disticntUncommonDigits(A, B)
|
Time Complexity: O(log10(A) + log10(B))
Auxiliary Space: O(log10(A) + log10(B))
Method #2:Using Operator.countOf() method
Python3
import operator as op
def disticntUncommonDigits(A, B):
A = str (A)
B = str (B)
lis1 = list ( map ( int , A))
lis2 = list ( map ( int , B))
res = []
for i in lis1:
if op.countOf(lis2, i) = = 0 :
res.append(i)
for i in lis2:
if op.countOf(lis1, i) = = 0 :
res.append(i)
res.sort(reverse = True )
for i in res:
print (i, end = " " )
A = 378212
B = 78124590
disticntUncommonDigits(A, B)
|
Time Complexity: O(log10(A) + log10(B))
Auxiliary Space: O(log10(A) + log10(B))
Approach#3: Using set() and difference() and sorted and union
Convert both numbers A and B to sets of digits. Find uncommon digits in both sets by taking the set difference and union of the sets. Convert the resulting set of uncommon digits to a list and sort it in descending order. Print the list of uncommon digits as a string.
Algorithm
1. Initialize A and B with given values.
2. Convert A and B to sets of digits.
3. Find uncommon digits using set difference and union.
4. Convert resulting set to a list and sort it in descending order.
5. Print the list of uncommon digits as a string.
Python3
A = 378212
B = 78124590
digits_A = set ( str (A))
digits_B = set ( str (B))
uncommon_digits = digits_A.difference(digits_B).union(digits_B.difference(digits_A))
uncommon_digits = sorted ( list (uncommon_digits), reverse = True )
print ( ' ' .join(uncommon_digits))
|
Time Complexity: O(m+n), where m and n are the number of digits in A and B, respectively. This is because the set() function and set difference and union operations take O(m) and O(n) time, respectively.
Space Complexity: O(m+n), where m and n are the number of digits in A and B, respectively. This is because we are storing the digits of A and B in sets, which take up O(m) and O(n) space, respectively. The resulting set of uncommon digits and the list of uncommon digits also take up O(m+n) space.
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...