forked from AdaGold/reverse-words
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathreverse_words.rb
44 lines (32 loc) · 995 Bytes
/
reverse_words.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# A method to reverse each word in a sentence, in place.
def string_reverse(my_string, beginning, ending)
front_index = beginning
back_index = ending
while front_index < back_index
front_value = my_string[front_index]
back_value = my_string[back_index]
my_string[front_index] = back_value
my_string[back_index] = front_value
front_index += 1
back_index -= 1
end
end
def reverse_words(my_words)
return if my_words == nil
i = 0
length = my_words.length
while i < length
while my_words[i] == ' ' && i < length
i += 1
end
beginning = i
while my_words[i] != ' ' && i < length
i += 1
end
ending = i - 1
string_reverse(my_words, beginning, ending)
end
return my_words
end
# time complexity: O(n) - goes through the loop n times, in this case it is through the length of my_words
# space complexity: O(1) - the storage doesn’t grow with the size of the input, stays constant with the length of the string