I was asked in an interview last week, couldn't figure the solution and also did not find it anywhere.
You are given char array and should do it inplace.
Lets ssay i have a string " . . . . . word1 . . . . word2 . . . .word3 . . . ." (Dot is a space here for representation)
I have to rearrange it in such a way the leading and trailing spaces are gone and the spaces should be distributed equally between the words.
Output:
"word1 . . . . . . . . . word2 . . . . . . . . word3"
5+4+4+4 number of spaces initially = 17 spaces
distribute between 3 words, so 9 + 8 (first words always get more sspaces to distribute it properly because the spaces cannot be distributed equally, if they can be distributed equally that is good.)