Given a integer K. Task is Print All binary string of size K (Given number).
Input : K = 3 Output : 000 , 001 , 010 , 100 , 101 Input : K = 4 Output :0000 0001 0010 0100 0101 1000 1001 1010
Idea behind that is IF string ends with ‘1’ then we put only ‘0’ at the end. IF string ends with ‘0’ then we put both ‘0’ and ‘1’ at the end of string for generating new string.
Below is algorithm
K : size of string First We Generate All string starts with '0' initialize n = 1 . GenerateALLString ( K , Str , n ) a. IF n == K PRINT str. b. IF previous character is '1' :: str[n-1] == '1' put str[n] = '0' GenerateAllString ( K , str , n+1 ) c. IF previous character is '0' :: str[n-1] == '0' First We Put zero at end and call function PUT str[n] = '0' GenerateAllString ( K , str , n+1 ) PUT str[n] = '1' GenerateAllString ( K , str , n+1 ) Second Generate all binary string starts with '1' DO THE SAME PROCESS
Below is recursive implementation.
000 001 010 100 101
This article is contributed by Nishant Singh . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Generate all binary strings from given pattern
- Generate all the binary strings of N bits
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Generate Binary Strings of length N using Branch and Bound
- Count number of binary strings without consecutive 1’s : Set 2
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Generate two output strings depending upon occurrence of character in input string.
- Generate two output strings depending upon occurrence of character in input string in Python
- Count of sub-strings with equal consecutive 0's and 1's
- Generate all the binary number from 0 to n
- Check if a binary string contains consecutive same or not
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Longest consecutive sequence in Binary tree
- Length of longest consecutive ones by at most one swap in a Binary String
- Maximum Consecutive Zeroes in Concatenated Binary String
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Binary string with given frequencies of sums of consecutive pairs of characters