Reverse Words in a String

Question

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

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

Tags

  • String

Thought

The solution is straightforward, the only thing should be noticed is that: Multiple consecutive space might be split by split(' ') function in python, so we need to filter the list after split the string.

Code

class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        string_list = s.split(' ')
        for i, item in enumerate(string_list)
        result = reversed(string_list)
        return ' '.join(result)

results matching ""

    No results matching ""