0

Expand a given string with "spaces" and "swaps"

  • Step 1: Expand with "spaces" from the center towards the start/end of the string in each step. i) string with no middle character adds a middle single space e.g. ""BATA" " becomes "BA TA" and becomes “B A T A” ii) string with middle character adds left and right space. A space is introduced before and after a ‘character’ or a ‘substring’. e.g. ""BATIA" " becomes "BA T IA" and becomes "B A T I A"

  • Step 2: Once space is added in each step then it is followed by character swap (swap between character after left space and character before right space). Swap happens and continues between all the opposite characters till the center (space or middle character) of the string is reached. e.g.
    "BA TA" becomes "BA TA" (swap for middle space will be same) "B A T A" becomes "B T A A" (swap between A and T, followed by continuous swap till the middle empty space)

    e.g. "BA T IA" becomes "BA T IA" (swap middle character T will be in the same position) "B A T I A" becomes “B I T A A” (swap between A and I, followed by continuous swap till the middle character ‘T’)

  • Step 3: Print the output

  • Step 4: Iterate from Step 1

Maruthi Answered question August 3, 2021