Skip to main content

557 - Reverse Words in a String III

Details

KeyValue
Linkhttps://leetcode.com/problems/reverse-words-in-a-string-iii/
LanguagePython 3
Runtime83 ms, faster than 36.40% of Python3 online submissions for Reverse Words in a String III
Memory Usage14.5 MB, less than 82.00% of Python3 online submissions for Reverse Words in a String III
Datastructuresstr
AlgorithmsTwo-Pointers
ComplexityTime: O(N) Memory: O(N) (C=number of characters in all words in array)

Procedure

  1. ...

Code

Option 1: Two-Pointers

class Solution:
def reverseWords(self, s: str) -> str:
left = right = 0
result = ''

while right < len(s):
if s[right] != ' ':
right += 1
else:
result += s[left:right+1][::-1]
left = right = right + 1

result += ' ' + s[left:right][::-1]
return result[1:]

Option 2: One-Liner

class Solution:
def reverseWords(self, s: str) -> str:
return " ".join(word[::-1] for word in s.split())