Sentence Reverse #135
Unanswered
esthicodes
asked this question in
Q&A
Replies: 1 comment
-
how to check if the time complexity is correct or not:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
You are given an array of characters arr that consists of sequences of characters separated by space characters. Each space-delimited sequence of characters defines a word.
Implement a function reverseWords that reverses the order of the words in the array in the most efficient manner.
Explain your solution and analyze its time and space complexities.
Example:
input: arr = [ 'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
'm', 'a', 'k', 'e', 's', ' ',
'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ]
output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
'm', 'a', 'k', 'e', 's', ' ',
'p', 'e', 'r', 'f', 'e', 'c', 't' ]
Constraints:
[time limit] 5000ms
[input] array.character arr
0 ≤ arr.length ≤ 100
[output] array.character
time complexity : O(N) 👍🏻 iterating from I to n.
inside the part doing the second loop, iterate every character once, this reverse basically once.
two n iterate two times.
space: O(1)
Beta Was this translation helpful? Give feedback.
All reactions