Distinct Numbers obtained by generating all permutations of a Binary String
Given a binary string S, the task is to print all distinct decimal numbers that can be obtained by generating all permutations of the binary string.
Examples:
Input: S = “110”
Output: {3, 5, 6}
Explanation:
All possible permutations are {“110”, “101”, “110”, “101”, “011”, “011”}.
Equivalent decimal numbers of these binary strings are {6, 5, 6, 5, 3, 3} respectively.
Therefore, the distinct decimal numbers obtained are {3, 5, 6}.Input: S = “1010”
Output: {3, 5, 6, 9, 10, 12}
Approach: The problem can be solved using a Set. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
Python3
|
{3, 5, 6, 9, 10, 12}
Time Complexity: O(N * N!)
Auxiliary Space: O(N * N!)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Original page link
Best Cool Tech Gadgets
Top favorite technology gadgets
0 comments:
Post a Comment