186. (Locked)Reverse Words in a String II

Difficulty: Medium

Frequency: N/A 

Similar to Question [151. Reverse Words in a String], but with the following constraints:

“The input string does not contain leading or trailing spaces and the words are always separated by a single space.”

Could you do it in-place without allocating extra space?


 

Solution 1:

data structure:

string

steps:

1. reverse the whole string

2. reverse the single word

complexity:

Runtime: O(n)

Space: O(1)

Code:

Challenge:

Rotate an array to the right by k steps in-place without allocating extra space. For instance, with k = 3, the array [0, 1, 2, 3, 4, 5, 6] is rotated to [4, 5, 6, 0, 1, 2, 3].


Things to learn:
Advertisements

One thought on “186. (Locked)Reverse Words in a String II

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s