A programmer needs to reverse the order of words in a given string, where words are separated by single spaces. For example, 'codesignal is awesome' should become 'awesome is codesignal'. Which of the following approaches is the MOST efficient in terms of time complexity for a typical programming language?
-
A
Split the string by spaces into an array of words, reverse the array, and then join the words back together with spaces.
-
B
Iterate through the string from end to start, building each word character by character, and append them to a new string.
-
C
Use a stack data structure. Push each word onto the stack, and then pop them off one by one to form the new string.
-
D
Reverse the entire string, and then iterate through the reversed string to reverse each individual word in place.