-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path557.2_reverse_words_in_string.rb
52 lines (39 loc) · 1.12 KB
/
557.2_reverse_words_in_string.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
45
46
47
48
49
50
51
52
# frozen_string_literal: true
# https://leetcode.com/problems/reverse-words-in-a-string-iii/
require_relative '../test_helper'
# Runtime: 140 ms, faster than 59.74% of Ruby online submissions for Reverse Words in a String III.
# Memory Usage: 211.7 MB, less than 77.92% of Ruby online submissions for Reverse Words in a String III.
class Solution
class << self
def reverse_words(s)
left = 0
chars = s.chars
chars.each_with_index do |char, right|
next unless char == ' '
reverse(chars, left, right - 1)
left = right + 1
end
reverse(chars, left, chars.size - 1)
chars.join
end
def reverse(chars, left, right)
while left < right
chars[left], chars[right] = chars[right], chars[left]
left += 1
right -= 1
end
end
end
end
class TestSolution < Minitest::Test
def test_1
s = "Let's take LeetCode contest"
output = "s'teL ekat edoCteeL tsetnoc"
assert_equal output, Solution.reverse_words(s)
end
def test_2
s = 'God Ding'
output = 'doG gniD'
assert_equal output, Solution.reverse_words(s)
end
end