![]() |
VOOZH | about |
Given a string s, create a new string such that it contains the characters of the two halves of the string s combined alternately in reverse order.
Examples:
Input : s = carbohydrates
Output : hsoebtraarcdyInput : s = sunshine
Output : sennuish
Explanation:
Example 1: Two halves of the string carbohydrate are carboh and ydrates. As they needed to be added in reverse alternately, start with h from first half then s from second half followed by o from first half, e from second half and so on. The string p comes out to be hsoebtraarcdy. If one of the string is completely finished then simply add the remaining characters of the other string in reverse order.
Example 2: The two halves of the string are suns and hine. String sennuish is the desired string p.
Algorithm:
Implementation:
sennuish
Time complexity: O(n) where n is the length of the string
Auxiliary Space: O(n)