Print all possible strings that can be made by placing spaces - GeeksforGeeks
Print all possible strings that can be made by placing spaces
Given a string you need to print all possible strings that can be made by placing spaces (zero or one) in between them.
Input: str[] = "ABC" Output: ABC AB C A BC A B C
Source: Amazon Interview Experience | Set 158, Round 1 ,Q 1.
We strongly recommend to minimize your browser and try this yourself first.
The idea is to use recursion and create a buffer that one by one contains all output strings having spaces. We keep updating buffer in every recursive call. If the length of given string is 'n' our updated string can have maximum length of n + (n-1) i.e. 2n-1. So we create buffer size of 2n (one extra character for string termination).
We leave 1st character as it is, starting from the 2nd character, we can either fill a space or a character. Thus one can write a recursive function like below.
Read full article from Print all possible strings that can be made by placing spaces - GeeksforGeeks
No comments:
Post a Comment