LeetCode 186 Reverse Words in a String II

LeetCode 186 Reverse Words in a String II Problem

Download Code
class Solution(object):
    def reverseWords(self, s):
        """
        :type s: a list of 1 length strings (List[str])
        :rtype: nothing
        """
        ls, pos = len(s), 0
        if s is None or ls == 0:
            return
        self.reverse(s, 0, ls)
        for i in range(ls + 1):
            if i == ls or s[i] == ' ':
                self.reverse(s, pos, i)
                pos = i + 1

    def reverse(self, array_s, begin, end):
        for i in range((end - begin) / 2):
            array_s[begin + i], array_s[end - i - 1] = array_s[end - i - 1], array_s[begin + i]
Download Reverse Words in a String II.py

List of all Reverse Words in a String II problems

Leetcode 186 Reverse Words in a String II problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.