moonlightshadow123
6/19/2017 - 10:10 AM

151. Reverse Words in a String

  1. Reverse Words in a String
class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        s = s.strip()
        wordList = s.split(' ')
        wordList = [each for each in wordList if each != '' ]
        if wordList == []:
            return ''
        reveList = wordList[::-1]
        return reduce(lambda x, y: x + ' ' + y,reveList)

https://leetcode.com/problems/reverse-words-in-a-string/#/description

Given an input string, reverse the string word by word.

For example, Given s = "the sky is blue", return "blue is sky the".